Лучший способ позвонить в сеть Volley несколько раз - PullRequest
0 голосов
/ 16 мая 2018

У меня есть одно требование, где я должен вызывать метод Volley POST около 300-500 раз.В посте я отправляю 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();
    }
}

Получение нехватки памяти erorr по адресу: RequestQueue requestQueue = Volley.newRequestQueue(this);

...