AsyncHttpClient in Runnable

616 views Asked by At

In my app I need to implement some kind of chat. But instead of using longpoll connections I am just sending get requests to server once every 5 seconds. Chat is not primary function in my app. I'm using Handler to post a Runnable, where in run method I'm using AsyncHttpClient. Here's code:

chatHandler = new Handler();
    chatRunnable = new Runnable() {
        @Override
        public void run() {
            final int pos = adapter.getCount();
            AsyncHttpClient client = new AsyncHttpClient();
            client.addHeader("X-Auth-token", User.getInstance(getApplicationContext()).getToken());
            client.addHeader("clienttype", "android");
            try {
                client.addHeader("clientvesrion", (getPackageManager().getPackageInfo(getPackageName(), 0)).versionName);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }

            RequestParams params = new RequestParams();
            params.add("todo_id", todo.getServerId() + "");
            params.add("from_id", messages.size() == 0 ? "0" : messages.get(messages.size() - 1).getId() + "");
            client.setTimeout(5000);

            client.get(getApplicationContext(), getString(R.string.server_path) + getString(R.string.path_messages_from_id), params, new AsyncHttpResponseHandler(){
                      ...
            });
        }
    };
    chatHandler.postDelayed(chatRunnable, 5000);

However, sometimes this crashes at client.get() with following error:

FATAL EXCEPTION: main
java.lang.OutOfMemoryError: thread creation failed
        at java.lang.VMThread.create(Native Method)
        at java.lang.Thread.start(Thread.java:1050)
        at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:913)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1295)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:81)
        at com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:893)
        at com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:612)
        at ru.dotcapital.controlmanager.ChatActivity$1.run(ChatActivity.java:115)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

Some referenses at google says that its happening when you have lots of activities running and not finished, and lots of AsyncTasks running in one time, but my ChatActivity is 3rd activity.User would call and I have no AsyncTasks running in time when the error appeared. Any suggestions?

1

There are 1 answers

2
Aleksandar Stojadinovic On BEST ANSWER

I can't see the broader context from this code, but based on your question where you said you are doing something every 5 seconds, is it possible you are doing this routine even when you don't have internet? I also don't see you closing the client using the close() method. Not calling close() does not free up resources even if there is no response, and than continuous calls just fill up the memory.