访问JSONRPC - 新手 [英] Access JSONRPC - Novice

查看:68
本文介绍了访问JSONRPC - 新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我没有得到任何回复我之前的问题(帮助球衣客户)我以为我会尝试不同的方法 - 所以这里。我有一个在我的局域网上运行的媒体服务器,它有一个jsonrpc API接口,它接受表格中的请求



As I haven't gotten any replies to my previous question ( help with jersey client ) I thought I'd try a different approach - so here goes. I have a media server running on my LAN which has a jsonrpc API interface, it accepts a request in the form

"{id=1, method=slim.request, params=["-",["artists","0","-1"]]}"





并返回json



我可以使用Jersey客户端成功访问它但想知道如何在不使用Jersey的情况下执行它,因为我希望我的程序能够可以在Android上使用,据我所知,有一个Jersey客户端可用。显然这将用Java编写。



And returns json

I can access it successfully using a Jersey client but would like to know how to do it without using Jersey as I want my program to be usable on Android which doesn't to my knowledge have a Jersey client available. Obviously this will be written in Java.

推荐答案

认为这很好地完成了工作

Got it figured this does the job nicely
package com.pjksolutions.jasonrpc;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HTTPUtils
{
	String Server;
	String Request;
	URL url;
	HttpURLConnection conn;


	public HTTPUtils()
	{
	}

	public HTTPUtils(String Server)
	{
		this();
		this.Server = Server;
	}


	private void InitServer()
	{
		try
		{
			this.url = new URL(this.Server);
			this.conn = (HttpURLConnection) this.url.openConnection();
			this.conn.setConnectTimeout(10000);
			this.conn.setReadTimeout(10000);
			this.conn.setRequestMethod("POST");

			this.conn.setDoOutput(true);
			this.conn.setDoInput(true);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}


    public String PostRequest(String Request)
    {

        this.Request = Request;

        try 
		{
			this.InitServer();
            DataOutputStream wr = new DataOutputStream(
				this.conn.getOutputStream());
            wr.writeBytes(this.Request);
            wr.flush();
            wr.close();

            InputStream is = conn.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();

            while ((line = rd.readLine()) != null)
			{
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();
        }
        catch (Exception e)
		{
			return "error";
		}
    }
}


这篇关于访问JSONRPC - 新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆