I'm making an app a part of which involves downloading data from my server in JSON format. I'm using HTTP Post to achieve the same. The JSON file I'm downloading is around 2-3 Kb in size. I'm doing all these operations on a different thread.
However, the performance of my app is very unpredictable. I tried debugging it and found that sometimes my app gets stuck at line where I'm creating HttpPost object and sometimes it hardly takes 1-2 seconds to execute that line. What can be the issue ? Can it be because my app is taking lot of memory ? One more thing, my app runs fine second time once I force close it. Thanks !
public String getJSONString(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{sb.append(line + "n");}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
}