I am currently developing an Android application and encounter the following problem. I am making an HTTP request to a server that is supposed to send me back XML content that I then parse. I noticed recurring errors while parsing long XML strings so I decided to display the result of my requests and discovered that the string (or the stream?) that I receive is randomly truncated. Sometimes I get the whole string, sometimes half, sometimes a third, and it seems to follow a certain pattern in the amount of characters that are truncated, what I mean by that is that I sometimes get 320 characters after a request then 156 after the next then 320 twice, then 156 again (these aren't the actual numbers but it follows a pattern).
Here is my code for the request and conversion of the InputStream into a string:
private String downloadUrlGet(String myurl) throws IOException {
InputStream is = null;
// Only display the first 20000 characters of the retrieved
// web page content.
int len = 20000;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/xml");
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
// Reads an InputStream and converts it to a String.
private String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
The length of the XML that I try to retrieve is much less than 20000. I tried to use HttpURLConnection.setChunkedStreamingMode() with 0 and various other numbers as parameter but it didn't change anything.
Thanks in advance for any suggestions.