1
我的Android應用程序需要通過webservice從外部數據庫中檢索大量數據。數據很大,約60000條記錄(〜21MB)。我使用的是AsyncHttpClient,但我從來沒有回到由OutOfMemoryError引起的onSuccess方法中的結果。Android:從Webservice中檢索大型Json數據 - 獲取OutOfMemoryError
public void getData(final Handler handler) {
if (Log.isLoggable(TAG, Log.INFO)) {
log.info("PreSynch: Start loading " + objectTypeName);
}
(new AsyncHttpClient()).get(presyncUrl, requestParams, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
responseResult(response);
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray result) {
responseResult(result);
}
private void responseResult(Object result) {
// Never reach this code
log.info(objectTypeName + " successfully loaded");
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
// some logs
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
// some logs
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
// some logs
}
});
}
的錯誤是:
E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-209
java.lang.OutOfMemoryError
at java.util.ArrayList.add(ArrayList.java:114)
at org.json.JSONArray.put(JSONArray.java:166)
at org.json.JSONTokener.readArray(JSONTokener.java:430)
at org.json.JSONTokener.nextValue(JSONTokener.java:103)
at com.loopj.android.http.JsonHttpResponseHandler.parseResponse(JsonHttpResponseHandler.java:216)
at com.loopj.android.http.JsonHttpResponseHandler$1.run(JsonHttpResponseHandler.java:120)
at java.lang.Thread.run(Thread.java:856)
您正在嘗試將整個文件加載到數組中......您的內存不足......可能不是那樣做的?或者你可以http://downloadmoreram.com;) –
你有沒有考慮過分批處理它?一次說1000條記錄?有了這些數據,我會擔心它會超時。 – b85411
你能告訴我你正在獲取哪種類型的數據嗎? – KishuDroid