Android Studio 1.5
I have built a java library that I copy into Android Studio. The java library works ok running in my test cases running on Linux. However, when I copy the library into Android studio and test it on actual devices. It fails to read all the JSON. Only ~half gets read. The JSON is only about 15238 bytes. However, I have other JSON which are smaller about 1000 bytes that work ok with this code.
Is there some kind of limit on Android?
I am using the httpUrlConnection
and POST
jsonString = readJSONInputStream(mHttpUrlconnection.getInputStream());
And the function:
private String readJSONInputStream(InputStream inputStream) {
try {
final int SIZE = 8024;
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), SIZE);
String line = "";
String jsonString = "";
while((line = reader.readLine()) != null) {
jsonString += line;
}
log.log(Level.INFO, "JSONSTRING: " + jsonString);
/* Success */
return jsonString;
}
catch(...) {
.
}
}
I have also tried using this. Which works in my test cases on Linux. But fails to read all the data on Android.
private String readJSONInputStream(InputStream inputStream) {
try {
String jsonString = IOUtils.toString(inputStream, "UTF-8");
/* Success */
return jsonString;
}
catch(...) {
.
}
}
Code that uses the HttpUrlConnection
try {
URL url = null;
HttpsURLConnection connection = null;
OutputStreamWriter outputStream = null;
int responseCode = -1;
url = new URL(cnn_url);
connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=utf8");
connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(true);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(15000); /* msecs i.e. 15 seconds */
/* Send POST request with JSON object */
outputStream = new OutputStreamWriter(connection.getOutputStream());
outputStream.write(jsonObject.toString(), 0, jsonObject.toString().length());
outputStream.flush();
log.log(Level.INFO, "Connected to server OK");
/* Get response */
responseCode = connection.getResponseCode();
log.log(Level.INFO, "Returned responseCode [ " + responseCode + " ]");
String jsonString = null;
if(responseCode == 200) {
/* Read contents of inputstream into a string to be returned */
jsonString = readJSONInputStream(connection.getInputStream());
if(jsonString == null) {
log.log(Level.SEVERE, "Failed to read connection input stream");
}
else {
log.log(Level.INFO, "jsonString: " + jsonString);
}
}
else {
log.log(Level.SEVERE, "Invalid response code [ " + responseCode + " ]");
}
return jsonString;
}
These functions work on Android when the JSON is less than ~1000 bytes. But for a large JSON I have which is ~15000 it only reads ~half. As when I print the output I only see half of the JSON string.
Is there something I am doing wrong?