OOM使用齐射

时间:2014-08-20 09:50:06

标签: android json out-of-memory gson

private void load() {
    // TODO Auto-generated method stub
    JsonObjectRequest coinReq = new JsonObjectRequest(url,
            null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    hidepDialog();
                    try {

                    jsonarray = response.getJSONArray("coins");
                    // Parsing json
                    for (int i=0; i < jsonarray.length() ;i++) {
                            JSONObject obj = jsonarray.getJSONObject(i);
                            Coin coin= new Coin();
                            coin.setCost(obj.getString("catagory1"));
                            coin.setThumbnailUrl(obj.getString("pict_url"));
                            coin.setLot(obj.getString("minimum_bid"));
                            coin.setDesc(obj.getString("title"));
                            coin_list.add(coin);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    hidepDialog();

                }
            });
    AppController.getInstance().addToRequestQueue(coinReq);
}

解析大型JSON数据时会发出内存错误。 有人可以给出一些解决方案来处理大型json数据的内存不足问题。

1 个答案:

答案 0 :(得分:0)

尝试这个:

 private void load() { 


    RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());

    String url = "API URL";
    Uri.Builder builder = Uri.parse(url).buildUpon();

    // builder.appendQueryParameter("STATUS", "1); //if you need to pass a param


    Log.d("ENTERED_IN_THE_INFO_BLOCK", builder.toString());

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, builder.toString(), null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject menuObject) {

            Log.d("ENTERED_IN_THE_CART_INFO_BLOCK", "success");
            Log.d("ENTERED_IN_THE_CART_INFO_BLOCK", menuObject.toString());

                hidepDialog(); 

            try {
                //String resultListString = menuObject.getString("result").trim();

                //JSONObject jsonObject = new JSONObject(resultListString);
                //JSONArray jsonArray = jsonObject.getJSONArray("coins");


                JSONArray jsonArray = menuObject.getJSONArray("coins");



                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject obj = jsonArray.getJSONObject(i);

                     ArrayList<String> coin = new ArrayList<String>();

                        //Coin coin= new Coin();
                        coin.setCost(obj.getString("catagory1"));
                        coin.setThumbnailUrl(obj.getString("pict_url"));
                        coin.setLot(obj.getString("minimum_bid"));
                        coin.setDesc(obj.getString("title"));
                        coin_list.add(coin);


                }

                Log.d("ENTERED_IN_THE_ORDER_BLOCK_ID - data", coin.toString());


            } catch (Exception ignored) {

                if (getActivity().getApplicationContext() != null) {
                    Toast.makeText(getActivity().getApplicationContext(), "ERROR:: " + ignored.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            if (error.getMessage() != null) {
                Log.d("Volley error", error.getMessage());
            }

            error.printStackTrace();

            Log.d("ENTERED_IN_THE_BLOCK", "failed");
            Log.d("ENTERED_IN_THE_BLOCK", "" + error);


        }
    });

    queue.add(jsObjRequest);
}