Android:从Webservice中检索大型Json数据 - 获取OutOfMemoryError

时间:2015-10-08 04:16:41

标签: android json web-services

我的Android应用程序需要通过webservice从外部数据库中检索大数据。数据量很大,有60000条记录(~21MB)。我使用的是AsyncHttpClient,但是我从来没有收到由OutOfMemoryError引起的onSuccess方法的结果。

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)

1 个答案:

答案 0 :(得分:1)

这是Android平台的限制,因此您必须选择其他设计,例如分页webservice调用。