Android outofmemoryerror while fetching json

时间:2015-07-13 21:05:22

标签: android json

i am trying to fetch data from json which size is only 400kb which is interesting when i am trying to get the data from json i get this error:

07-13 19:09:59.311: E/art(2854): Throwing OutOfMemoryError "Failed to allocate a 966540 byte allocation with 777308 free bytes and 759KB until OOM"
07-13 19:09:59.316: E/AndroidRuntime(2854): FATAL EXCEPTION: Thread-206
07-13 19:09:59.316: E/AndroidRuntime(2854): Process: com.wunderlist.slidinglayersample, PID: 2854
07-13 19:09:59.316: E/AndroidRuntime(2854): java.lang.OutOfMemoryError: Failed to allocate a 966540 byte allocation with 777308 free bytes and 759KB until OOM
07-13 19:09:59.316: E/AndroidRuntime(2854):     at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:163)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at java.lang.StringBuilder.append(StringBuilder.java:311)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at org.json.JSONTokener.nextString(JSONTokener.java:224)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at org.json.JSONTokener.nextValue(JSONTokener.java:107)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at org.json.JSONTokener.readObject(JSONTokener.java:385)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at org.json.JSONTokener.nextValue(JSONTokener.java:100)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at org.json.JSONTokener.readArray(JSONTokener.java:430)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at org.json.JSONTokener.nextValue(JSONTokener.java:103)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at org.json.JSONArray.<init>(JSONArray.java:92)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at org.json.JSONArray.<init>(JSONArray.java:108)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at com.android.volley.toolbox.JsonArrayRequest.parseNetworkResponse(JsonArrayRequest.java:50)
07-13 19:09:59.316: E/AndroidRuntime(2854):     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:121)

The main idea is when i press the button i get 10 strings from json this is the code what happens in onClick() method:

private void makeJsonArrayRequest() {

    //showpDialog();

    JsonArrayRequest req  = new JsonArrayRequest(urlJsonArry,new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {

            Log.d(TAG, response.toString());

            try {
                // Parsing json array response
                // loop through each json object
                jsonResponse = "";
                for (int i = 1; i < 2; i++) {

                    //response.length()
                    JSONObject person = (JSONObject) response
                            .get(i);

                    String name = person.getString("nosaukums");
                    String email = person.getString("email");


                    JSONObject phone = person.getJSONObject("phone");
                    String home = phone.getString("latCo");
                    String mobile = phone.getString("longCo");

                    jsonResponse += "Name: " + name + "\n\n";
                     jsonResponse += "Email: " + email + "\n\n";
                            jsonResponse += "Home: " + home + "\n\n";
                            jsonResponse += "Mobile: " + mobile + "\n\n\n";

                }



                txtResponse.setText(jsonResponse);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }

            // hidepDialog();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            // hidepDialog();
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);
}

Do you have any suggestion or recommendations how to solve the problem which i have because i have tried so many things but nothing works

Update.

The jsonArray:

[
    {
    "name" : "Ravi Tamada", 
    "email" : "ravi8x@gmail.com",
    "phone" : {
        "home" : "08947 000000",
        "mobile" : "9999999999"
    }
    },
    {
    "name" : "Tommy", 
    "email" : "tommy@gmail.com",
    "phone" : {
        "home" : "08946 000000",
        "mobile" : "0000000000"
    }
    }
]

2 个答案:

答案 0 :(得分:0)

尝试将android:largeHeap="true"添加到清单文件中。

    <application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:largeHeap="true" >

或使用jackson json库。

希望这可能有所帮助:)

答案 1 :(得分:0)

我认为您的问题来自

jsonResponse = "";

为什么要将响应分配给一个空字符串?为什么你有一个如上所述的for循环,当然你想得到数组中的第一个元素

JSONObject person =(JSONObject)jsonResponse.getJSONObject(0)