views:

22

answers:

1

When I call a web service data does not come in the proper way. Some blocks are there string, integer, chars are there means mixed data in json form.

1) When I use this approch to convert the data...

        StringBuffer sb = new StringBuffer();            
        byte[] buf = new byte[256];
        int n = 0;                 

        while ((n = StrReader.read(buf)) > 0)
            {
                sb.append(new String(buf,0,n));
            }
            String returnContent = sb.toString();
            System.out.println(new String(returnContent));
            StrReader.close();

        }

output...

        text/htmlj
~"115.252.128.200", "roles": { "1": "anonymous user" }, "session": "", "cache": 0 } } }
No stack trace

2) and when I use this approch to convert the data...

 dis = new DataInputStream(hc.openInputStream());

        byte[] data1 = new byte[20];
        int len = 0;
        StringBuffer strBuffer = new StringBuffer();
        while ( -1 != (len = dis.read(data1)) )
        {
            received = new String(data1, 0, len);                                
            System.out.println(received);

        }

OUTPUT....

        text/html
j
~Salse, "#data": { "se
ssid": "fef51cf48aca
46e3b3aedafc02860f25
", "user": { "uid":
0, "hostname": "115.
252.128.200", "roles
": { "1": "anonymous
 user" }, "session":
 "", "cache": 0 } }
}
Outer---->>>}
No stack trace

NOTE.... the 'received' variable loses our data when it come out of loop...

+1  A: 

The question appears to be: why does the local variable 'received' have only a fragment of the text that is printed to the console. It is because the variable is assigned a new string for each batch of bytes that are read from the DataInputStream.

This code does not appear to concisely get the job done - why use a DataInputStream for example - so perhaps asking about the larger task at hand will be more useful.

Michael Donohue