android.os.NetworkOnMainThreadException in onHandleIntent method of IntentService -
i'm using broadcastreceiver
, intentservice
background operations , pass data activity
. know, intentservice
executing in different ui
thread, nevertheless i've got android.os.networkonmainthreadexception
method registering broadcastreceiver :
private void registerreceiver() { // создаем broadcastreceiver bcarbroadcast = new broadcastreceiver() { // действия при получении сообщений public void onreceive(context context, intent intent) { int status = intent.getintextra(constants.car_search_status, 0); int task = intent.getintextra(constants.car_search_task, 0); log.d(tag, "onreceive: task = " + task + ", status = " + status); if (status == constants.status_running) { beginprogresstask(); } if (status == constants.status_finished) { string data = intent.getstringextra(constants.car_search_data); if(data!=null) car_search_result_str.settext(data); } } }; intentfilter intfilt = new intentfilter(constants.broadcast_action); registerreceiver(bcarbroadcast, intfilt); }
oncreate :
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.car_search_activity); registerreceiver(); intent a= new intent(this, orderstateservice.class); startservice(a); }
and intentservice
:
public class orderstateservice extends intentservice { private static final string tag = "orderstateservice"; private jsonobject jsonresponse; public orderstateservice() { super("orderstateservice"); } @override public int onstartcommand(intent intent, int flags, int startid) { onhandleintent(intent); return start_redeliver_intent; } @override public void onstart(intent intent, int startid) { onhandleintent(intent); } @override protected void onhandleintent(intent intent) { intent intentresult = new intent(constants.broadcast_action); intent.putextra(constants.car_search_status, constants.status_running); sendbroadcast(intentresult); final bundle data = new bundle(); defaulthttpclient httpclient = new defaulthttpclient(); string url = constants.host_url + "/api/version/";//constants.order_url + intent.getstringextra(constants.order_id); log.i(tag, url); httpget httpget = new httpget(url); // try , catch leaving out try { httpget.addheader("authorization", sharedprefssingleton.getinstance().getsharedprefs().getstring(constants.user_authorization, null)); httpget.addheader("content-type", "application/json"); httpget.addheader("accept", "application/json; charset=utf-8"); httpclient.execute(httpget); httpresponse response = (httpresponse) httpclient.execute(httpget); ; log.i(tag + " code", integer.tostring(response.getstatusline().getstatuscode())); httpentity entity = response.getentity(); if (entity != null) { // read content stream inputstream instream = entity.getcontent(); header contentencoding = response.getfirstheader("content-encoding"); if (contentencoding != null && contentencoding.getvalue().equalsignorecase("gzip")) { instream = new gzipinputstream(instream); } // convert content stream string string resultstring = utils.getstringfrominputstream(instream); instream.close(); response.getentity().consumecontent(); // transform string jsonobject jsonresponse = new jsonobject(resultstring); log.i(tag, jsonresponse.tostring()); if (jsonresponse.has("order_car_info")) { data.putstring(constants.receiver_data, "error"); } else { data.putstring(constants.receiver_data, jsonresponse.tostring()); intent.putextra(constants.car_search_status, constants.status_finished); intent.putextra(constants.car_search_data, jsonresponse.tostring()); sendbroadcast(intent); } } } catch (exception e) { e.printstacktrace(); } } }
back trace :
android.os.networkonmainthreadexception 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1145) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ libcore.io.blockguardos.connect(blockguardos.java:84) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ libcore.io.iobridge.connecterrno(iobridge.java:127) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ libcore.io.iobridge.connect(iobridge.java:112) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ java.net.plainsocketimpl.connect(plainsocketimpl.java:459) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ java.net.socket.connect(socket.java:843) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory.java:119) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:144) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ library.orderstateservice.onhandleintent(orderstateservice.java:59) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ library.orderstateservice.onstartcommand(orderstateservice.java:31) 12-06 03:00:02.888 22280-22280/codenest.testaplication w/system.err﹕ @ android.app.activitythread.handleserviceargs(activitythread.java:2702) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ android.app.activitythread.access$2100(activitythread.java:135) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ android.app.activitythread$h.handlemessage(activitythread.java:1293) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ android.os.handler.dispatchmessage(handler.java:102) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ android.os.looper.loop(looper.java:136) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ android.app.activitythread.main(activitythread.java:5017) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ java.lang.reflect.method.invokenative(native method) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ java.lang.reflect.method.invoke(method.java:515) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 12-06 03:00:02.898 22280-22280/codenest.testaplication w/system.err﹕ @ dalvik.system.nativestart.main(native method)
removing
@override public int onstartcommand(intent intent, int flags, int startid) { onhandleintent(intent); return start_redeliver_intent; } @override public void onstart(intent intent, int startid) { onhandleintent(intent); }
part of code resolved issue.
thanks mike
Comments
Post a Comment