Tuesday 15 April 2014

java - AsyncHttpClient in Runnable -



java - AsyncHttpClient in Runnable -

in app need implement kind of chat. instead of using longpoll connections sending get requests server 1 time every 5 seconds. chat not primary function in app. i'm using handler post runnable, 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"); seek { client.addheader("clientvesrion", (getpackagemanager().getpackageinfo(getpackagename(), 0)).versionname); } grab (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, crashes @ client.get() next error:

fatal exception: main java.lang.outofmemoryerror: thread creation failed @ java.lang.vmthread.create(native method) @ java.lang.thread.start(thread.java:1050) @ java.util.concurrent.threadpoolexecutor.addworker(threadpoolexecutor.java:913) @ java.util.concurrent.threadpoolexecutor.execute(threadpoolexecutor.java:1295) @ java.util.concurrent.abstractexecutorservice.submit(abstractexecutorservice.java:81) @ com.loopj.android.http.asynchttpclient.sendrequest(asynchttpclient.java:893) @ com.loopj.android.http.asynchttpclient.get(asynchttpclient.java:612) @ ru.dotcapital.controlmanager.chatactivity$1.run(chatactivity.java:115) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:176) @ android.app.activitythread.main(activitythread.java:5365) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) @ dalvik.system.nativestart.main(native method)

some referenses @ google says happening when have lots of activities running , not finished, , lots of asynctasks running in 1 time, chatactivity 3rd activity.user phone call , have no asynctasks running in time when error appeared. suggestions?

i can't see broader context code, based on question said doing every 5 seconds, possible doing routine when don't have internet? don't see closing client using close() method. not calling close() not free resources if there no response, , continuous calls fill memory.

java android handler runnable asynchttpclient

No comments:

Post a Comment