this question has answer here:
i'm trying check internet connectivity android app keep running in problems.
i'm not looking code tests available network connection - i've got bit working - test whether can reach internet site or not.
(i appreciate if behind system presents logon screen instead of requested site, may not exact result want, handle later)
thanks following question think i've made progress, when run app crashes out (error info below).
the code have far follows (and must admit find try/catch stuff bit puzzling , tedious :-/ )
static public boolean isinternetreachable() { int statuscode = -1; try{ url url = new url("http://www.google.com/humans.txt"); httpurlconnection http = (httpurlconnection) url.openconnection(); statuscode = http.getresponsecode(); http.disconnect(); } catch (malformedurlexception ex) { return false; } catch (ioexception ex) { return false; } if (statuscode == httpurlconnection.http_ok) { return true; } else { //connection not ok return false; } }
i'm sure there neater ways , general advice welcome.
the error i'm getting when app crashes is:
01-24 19:53:14.767 10617-10617/com.nooriginalthought.bluebadgeparking e/androidruntime: fatal exception: main process: com.nooriginalthought.bluebadgeparking, pid: 10617 java.lang.runtimeexception: unable start activity componentinfo{com.nooriginalthought.bluebadgeparking/com.nooriginalthought.bluebadgeparking.preloadchecks}: android.os.networkonmainthreadexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2411) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2474) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1359) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:155) @ android.app.activitythread.main(activitythread.java:5696) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1028) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:823) caused by: android.os.networkonmainthreadexception @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1155) @ java.net.inetaddress.lookuphostbyname(inetaddress.java:418) @ java.net.inetaddress.getallbynameimpl(inetaddress.java:252) @ java.net.inetaddress.getallbyname(inetaddress.java:215) @ com.android.okhttp.hostresolver$1.getallbyname(hostresolver.java:29) @ com.android.okhttp.internal.http.routeselector.resetnextinetsocketaddress(routeselector.java:236) @ com.android.okhttp.internal.http.routeselector.next(routeselector.java:124) @ com.android.okhttp.internal.http.httpengine.connect(httpengine.java:272) @ com.android.okhttp.internal.http.httpengine.sendrequest(httpengine.java:211) @ com.android.okhttp.internal.http.httpurlconnectionimpl.execute(httpurlconnectionimpl.java:373) @ com.android.okhttp.internal.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:323) @ com.android.okhttp.internal.http.httpurlconnectionimpl.getresponsecode(httpurlconnectionimpl.java:491) @ com.nooriginalthought.bluebadgeparking.preloadchecks.isinternetreachable(preloadchecks.java:41) @ com.nooriginalthought.bluebadgeparking.preloadchecks.oncreate(preloadchecks.java:70) @ android.app.activity.performcreate(activity.java:5958) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1129) @ android.app.activitythread.performlaunchactivity(activitythread.java:2364) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2474) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1359) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:155) @ android.app.activitythread.main(activitythread.java:5696) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1028) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:823)
as david mentioning in comments, should google exception name , try turnaround yourself.
by looking @ stackoverflow answer referring to, need make network communications outside main thread. used way creating asynctask.
in case, (you can create new internettask.java or append current mainactivity.java) like:
class internettask extends asynctask<void, void, boolean>{ private mainactivity activity; internettask(mainactivity activity){ this.activity = activity; } @override protected boolean doinbackground(void... params) { int statuscode = -1; try{ url url = new url("http://www.google.com/humans.txt"); httpurlconnection http = (httpurlconnection) url.openconnection(); statuscode = http.getresponsecode(); http.disconnect(); } catch (malformedurlexception ex) { return false; } catch (ioexception ex) { return false; } if (statuscode == httpurlconnection.http_ok) { return true; } else { //connection not ok return false; } } @override protected void onpostexecute(boolean aboolean) { super.onpostexecute(aboolean); activity.receivemagic(aboolean); } }
then, need add new public method in activity receive boolean in mainactivity.
public void receivemagic(boolean isgood){ if (isgood){ toast.maketext(mainactivity.this, "it good", toast.length_short).show(); }else { toast.maketext(mainactivity.this, "it not connected", toast.length_short).show(); } }
and need call new asynctask activity with:
new internettask(this).execute();
make sure add internet permission manifest also.
Comments
Post a Comment