broadcastreceiver - W/System.err(21922): android.os.NetworkOnMainThreadException -
i have checkboxlist user selectes items envloping selecting in json format firing json string alarmmanager getllrd class. have problem receiving intent in intentservice
class since getting intent in onhandleintent not every 60 second various time shows in output below.
i have tried intentreceiver there getting output scheduled. therefor, want start httpurlconenction
onreceive method in intentreceiver
. have tried getting warning android.os.networkonmainthreadexception
dont have problem internet conenction since have asyntask classes sending , getting requests to/from server in app.
can send httputlconenction request broadcastreceiver , doing wrong?
some of output:
07-07 19:39:06.805: i/system.out(7534): test onhandleintent{"selected":[6,9]} 07-07 19:39:19.417: i/system.out(7534): test onhandleintent{"selected":[6]} 07-07 19:39:19.417: i/system.out(7534): test onhandleintent{"selected":[6,9]} 07-07 19:39:30.378: i/system.out(7534): test onhandleintent{"selected":[6,9]} 07-07 19:39:45.323: i/system.out(7534): test onhandleintent{"selected":[6,9]}
mainactivity class:
intent intent = new intent(mainactivity.this, intentreceiver.class); intent.putextra("json_data", json); pendingintent pendingintent = pendingintent.getservice( getapplicationcontext(), 3, intent, pendingintent.flag_update_current); alarmmanager alarm = (alarmmanager) getsystemservice(context.alarm_service); calendar cal = calendar.getinstance(); alarm.setrepeating(alarmmanager.rtc_wakeup, system.currenttimemillis(), 60 * 1000, pendingintent); // cal.gettimeinmillis() startservice(intent);
getllrd class:
public class getllrd extends intentservice { public getllrd() { super("intentservice"); } @override protected void onhandleintent(intent intent) { string jsonstring = intent.getstringextra("json_data"); system.out.println("test onhandleintent" + jsonstring); if(jsonstring != null){ system.out.println("test"); } } }
intentreceiver:
public class intentreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { try { string action = intent.getstringextra("json_data"); if (!action.isempty()) { system.out.println("test intentreiceier" + action); bufferedreader reader = null; try { url myurl = new url( "https://apple-bustracker.rhcloud.com/webapi/test"); httpurlconnection conn = (httpurlconnection) myurl .openconnection(); conn.setrequestmethod("post"); conn.setdooutput(true); conn.setconnecttimeout(10000); conn.setreadtimeout(10000); conn.setrequestproperty("content-type", "application/json"); conn.connect(); // create data output stream dataoutputstream wr = new dataoutputstream( conn.getoutputstream()); // write output stream string wr.writebytes(jsonstring); wr.close(); stringbuilder sb = new stringbuilder(); reader = new bufferedreader(new inputstreamreader( conn.getinputstream())); string line; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } try { gson gson = new gson(); type listtype = new typetoken<list<itemdto>>() { }.gettype(); data = gson.fromjson(sb.tostring(), listtype); } catch (jsonsyntaxexception e) { e.printstacktrace(); } (itemdto itemdto : data) { double latitude = itemdto.getlatitude(); double longitude = itemdto.getlongitude(); int route = itemdto.getroute(); string direction = itemdto.getdirection(); system.out.println("test" + latitude + ", " + longitude + ", " + ", " + route + ", " + direction); } } catch (ioexception e) { e.printstacktrace(); } { if (reader != null) { try { reader.close(); } catch (exception e) { e.printstacktrace(); } } } } } catch (exception e) { } } }
can send httputlconenction request broadcastreceiver
no. onreceive()
called on main application thread, , should not disk i/o or network i/o on main application thread. move http code intentservice
, , call startservice()
on intentservice
onreceive()
.
Comments
Post a Comment