android - NetworkOnMainThreadException in IntetentService -
i have intentservice network action (http post) shows networkonmainthreadexception. if right, intentservice running on separate thread. can tell why exception thrown? code is:
public class updateservice extends intentservice { public static final int update_progress = 8344; broadcastreceiver broadcastreceiver; public updateservice() { super("updateservice"); } @override protected void onhandleintent(intent intent) { if (broadcastreceiver == null) { broadcastreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { bundle extras = intent.getextras(); networkinfo info = (networkinfo) extras.getparcelable("networkinfo"); state state = info.getstate(); if (state == state.connected) { onnetworkup(); } else { } } }; final intentfilter intentfilter = new intentfilter(); intentfilter.addaction(connectivitymanager.connectivity_action); registerreceiver(broadcastreceiver, intentfilter); } } @override public void ondestroy(){ unregisterreceiver(broadcastreceiver); } void onnetworkup(){ string adatarow = ""; seek { file myfile = new file( environment.getexternalstoragedirectory().getpath() + "/myfile.txt"); filereader fr = new filereader(myfile); bufferedreader myreader = new bufferedreader(fr); while ((adatarow = myreader.readline()) != null) updatelyne(adatarow); // fr.close(); } grab (exception e) { log.e("onnetworkup",e.tostring()); } void updatelyne(string adatarow){ jsonparser jsonparser = new jsonparser(); string[] words = adatarow.split(" "); pid = words[1]; string rtime = adatarow; list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair(tag_pid, pid)); params.add(new basicnamevaluepair(tag_time, rtime)); jsonobject json = null; if (words[0].equalsignorecase("cancel")){ json = jsonparser.makehttprequest(url_cancel, "post", params); } else{ log.d("empty","file empty!!"); } seek { int success = json.getint("success"); if (success == 1) { delline(adatarow); // delete line } else { log.d("pid="+pid+" not on board", "failed in deleting"); } } grab (jsonexception e) { e.printstacktrace(); } } }
the jsonparser.java given below
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request seek { // check request method if(method == "post"){ defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } grab (unsupportedencodingexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } seek { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } grab (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // seek parse string json object seek { jobj = new jsonobject(json); } grab (jsonexception e) { log.e("json parser", "error parsing info " + e.tostring()); } // homecoming json string homecoming jobj; } }
the problem you're not running network request in onhandleintent()
, you're creating instance of broadcastreceiver
, registering it. actual network request won't run there. run when broadcastreceiver
receives message, happens on ui thread. right scheme here create broadcastreceiver
somewhere else, , when message received, start intentservice
, executes network phone call within onhandleintent()
- indeed run on worker thread. hope helps.
android intentservice networkonmainthread
No comments:
Post a Comment