Reputation: 22526
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Hello,
I am using the httpUrlConnection
to retrieve a json string from a webservice. Then I get the inputStream
from the connection
jsonString = readJSONInputStream(mHttpUrlconnection.getInputStream());
I then use the following function to read the inputstream to get the JSON.
private String readJSONInputStream(final InputStream inputStream) {
log.log(Level.INFO, "readJSONInputStream()");
Reader reader = null;
try {
final int SIZE = 16092;
char[] buffer = new char[SIZE];
int bytesRead = 0;
int read = 0;
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), SIZE);
bytesRead = reader.read(buffer);
String jsonString = new String(buffer, 0, bytesRead);
log.log(Level.INFO, "bytesRead: " + bytesRead + " [ " + jsonString + " ]");
/* Success */
return jsonString;
}
catch(IndexOutOfBoundsException ex) {
log.log(Level.SEVERE, "UnsupportedEncodingexception: " + ex.getMessage());
}
catch(IOException ex) {
log.log(Level.SEVERE, "IOException: " + ex.getMessage());
}
finally {
/* close resources */
try {
reader.close();
inputStream.close();
}
catch(IOException ex) {
log.log(Level.SEVERE, "IOException: " + ex.getMessage());
}
}
return null;
}
However, if the json is small say 600 bytes
then everything is ok (so the code above does work for smaller data). But I have some JSON that is about 15000 bytes
in size so I set the maximum size to 16092
.
However, the JSON it only reads about about 6511
and just cuts off.
I don't understand that if the JSON is small there is no problem. But for the larger JSON it just cuts off at the same size each time.
Am I doing anything wrong here. Anything I should check.
Many thanks for any suggestions,
Upvotes: 0
Views: 1201
Reputation: 321
Try the following code:
String readResponse(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
Upvotes: 3
Reputation: 1046
You should continue to read from the buffer until bytesRead = -1.
I suspect that the reader is buffering the data and only returning a certain amount of data at a time.
Try looping until bytesRead == -1 with each loop append the read data to your resultant String.
Upvotes: 1
Reputation: 4305
Most likely your code is broken here:
bytesRead = reader.read(buffer);
You should have cycle around read. I think you do not read all chars from stream.
Upvotes: 2