0

I am trying to get response from XBMC:

try {
        Socket socket = new Socket("192.168.1.2",9090);

        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeBytes("{\"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Introspect\", \"id\": 1}");

        DataInputStream is = new DataInputStream(socket.getInputStream());
        byte ab[] = new byte[1024];
        is.read(ab, 0, ab.length);
        String abc = new String(ab);
        System.out.println(abc);
} catch (Exception e) {         
    e.printStackTrace();
}

But the response string is too long, I change 1024 to 60000 and this byte array still can not contain the whole response string. How to solve this?

0

This question does not belong to Raspberry Pi, it's more a common Java programming question. To be able to read large streams, use loop:

StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[100];
int numberRead = 0;
do {
    numberRead = is.read(buffer);
    if (numberRead != -1){
       // do work here
       sb.append(buffer);
    }
} while (numberRead == buffer.length);

Not the answer you're looking for? Browse other questions tagged or ask your own question.