Grace
Grace

Reputation: 11

Out of memory error using Volley using timers

I have this issue with Volley library requests. I make calls to API and since they have to be updated very often I implemented timers. After some time it throws me error:"Throwing OutOfMemoryError “pthread_create (1040KB stack) failed: Try again. I used the method which was suggested in this post: Throwing OutOfMemoryError "pthread_create (1040KB stack) failed: Try again" when doing asynchronous posts using Volley , but for me it did not work, the data coming from API stopped updating. I thought it might be something with timers or it is just something I am doing wrong using Volley.

If you have any idea, feel welcome to share.

My printer class

    public void setMenuInformation(String url, Context context, final VolleyCallback callback) {
    Log.d("API", "Currently calling URL " + url);

    if (eRequestQueue == null) {
        eRequestQueue  = Volley.newRequestQueue(context);
        eRequestQueue.add(new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                String temp = response.substring(2, response.length() - 2);
                Log.d("API", "Current menu" + response);
                byte msgArray[];
                try {
                    msgArray = temp.getBytes("ISO-8859-1");
                    currentMenu = msgArray[0];
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }



                menuInformation = response;
                callback.onSuccess(menuInformation);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("API", "Current menu Nope ");
                callback.onFailure(error);
            }
        }));
    }
}

Main Activity:

  myPrinterDetails.setMenuInformation("http://" + nsdServiceInfo.getHost() + "/GetCurrentMenu", 
 MainActivity.this, new PrinterNew.VolleyCallback() {
                @Override
                public void onSuccess(String result) {

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("NSD", "Current menu response succeded and added to array " + nsdServiceInfo);
                            //services.add(myPrinterDetails);
                            mAdapter.notifyDataSetChanged();

                        }
                    });
                    Log.d("NSD", "CurrentMenu " + myPrinterDetails.getMenuInformation());

                    myTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    myPrinterDetails.setMenuInformation("http://" + nsdServiceInfo.getHost() + "/GetCurrentMenu", MainActivity.this, new PrinterNew.VolleyCallback() {
                                        @Override
                                        public void onSuccess(String result) {

                                            Log.d("NSD", "Current menu  response added to timer  " + nsdServiceInfo);
                                            mAdapter.notifyDataSetChanged();
                                        }


                                        @Override
                                        public void onFailure(Object response) {
                                            Log.d("NSD", "Current menu timer  failed");
                                        }
                                    });
                                }
                            });
                        }
                    }, 0, 2000);

                }

                @Override
                public void onFailure(Object response) {
                    Log.d("NSD", "Current menu response failed");
                }
            });

        }
    };
}

Upvotes: 1

Views: 176

Answers (0)

Related Questions