需要您的帮助才能从 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();
}
最佳答案
您可以使用 Google Volley为您建立网络。在许多其他方面,它有一个内置的方法来检索 JSON 对象,无论大小。
试一试。
关于android - 在 Android 中从 Httpclient 读取大量数据(7MB 或更多)时需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18480875/