Kodi Community Forum

Full Version: Help with JSON/Java
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I was wondering how/where the data is "returned" in response to a JSON-RPC (via TCP) method? I have managed to call a method successfully in my crummy java program:

Code:
JSONObject js = new JSONObject();

js.put("jsonrpc", "2.0");
js.put("method", "Input.Back");

Socket client = new Socket(hostname, port)
Writer w = new OutputStreamWriter(client.getOutputStream());
js.write(w);
w.flush();

But I don't see any "return" data in the outputstream of the "client" socket. This may not be relevant to Kodi in general, but thought I'd ask for help anyway. I'm very new to programming, so the simpler the better!

Thanks

Edit: rest of code:

Code:
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

String x;
while ((x = in.readLine()) != null) {
    System.out.println(x);
}
i personally dont know java, though check out stackoverflow, you can post general programming questions there and get quality answers fast, very good site
Every JSON-RPC request needs an id (can be anything):
Code:
> {"jsonrpc":"2.0","method":"Input.Back","id":1}
< {"id":1,"jsonrpc":"2.0","result":"OK"}

This instead doesn't return anything:
Code:
> {"jsonrpc":"2.0","method":"Input.Back"}
(2015-10-29, 20:07)axa88 Wrote: [ -> ]i personally dont know java, though check out stackoverflow, you can post general programming questions there and get quality answers fast, very good site

Yep, that is my next move, but I feel like the questions there need to be more general and just better informed. Plus I didn't already have an account Tongue


(2015-10-29, 20:28)menakite Wrote: [ -> ]Every JSON-RPC request needs an id (can be anything):
Code:
> {"jsonrpc":"2.0","method":"Input.Back","id":1}
< {"id":1,"jsonrpc":"2.0","result":"OK"}

This instead doesn't return anything:
Code:
> {"jsonrpc":"2.0","method":"Input.Back"}

Thanks, I added that, but there is still no response. I'm not sure if I'm looking for the response in the right place, or even the right way:

Code:
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

String x;
while ((x = in.readLine()) != null) {
    System.out.println(x);
}
Fixed it, and it was definitely something I should have picked up on earlier. The "readLine" method waits for a new line char before actually printing anything, and thus does nothing since there isn't one in the return data. Changing it to "read()" and casting the int to a char works great!