需要您的帮助才能从 Android 中的 httpclient 获取 json 响应,因为下面提到的代码会使应用程序在 android 设备上崩溃,特别是在 GingerBread 设备上,因为 JSON 响应的大小非常大(可能是 7 MB)。所以我想知道从 Httpclient 读取 JSONresponse 的任何替代方法,因为目前的实现消耗了太多内存并使我的应用程序在低端设备上崩溃。
对于解决此问题的任何建议或帮助将非常有用。
HttpClient httpClient = new DefaultHttpClient(ccm, params);
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Cache-Control", "no-cache; no-store");
HttpResponse httpResponse = httpClient.execute(httpGet);
response = Utils.convertStreamToString(httpResponse.getEntity().getContent());
public static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
//System.gc();
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}