多次拨打Volley网络电话的最佳方式

时间:2018-05-16 08:15:38

标签: android android-volley

我有一个要求,我必须在大约300-500次附近调用Volley POST方法。在帖子中我发送JSONObject并作为响应我得到String作为输出。但我的问题是当我在某些设备上拨打凌空时我得到java.lang.OutOfMemoryError因为呼叫服务超过300次。以下是我的代码:

for (int i=0;i<myJsonArr.length();i++) // here myJsonArr  may contains 300-500 json object
        {
            customRequestTest(allAppJson.getJSONObject(i));
        }


public void customRequestTest(JSONObject jsonObject)
{
    try {
        String myURL = "My URL";
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        final String mRequestBody = jsonObject.toString();
        System.out.println (">>>>>>>>>>mRequestBody"+mRequestBody);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, myURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("LOG_RESPONSE", response);
                System.out.println (">>>>>>>>onResponse"+response +" for request "+mRequestBody);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("LOG_RESPONSE", error.toString());
                System.out.println (">>>>>>>>onErrorResponse"+error.toString()+" for request "+mRequestBody);
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                    return null;
                }
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String responseString = "";
                if (response != null) {
                    String str = new String(response.data);
                    responseString = str;
                }
                return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
            }
        };

        requestQueue.add(stringRequest);
    } catch (Exception e) {
        System.out.println (">>>>>>>>Inside catch of customRequestTest");
        e.printStackTrace();
    }
}

RequestQueue requestQueue = Volley.newRequestQueue(this);

中出现内存错误

0 个答案:

没有答案