i trying retrieve data website , app keeps crashing. have located issue down openstream() command. class using defined below. unable figure out doing wrong. advise appreciated.
private class myasynctask extends asynctask<void, void, void>{ //execute on background (out of ui thread) protected void doinbackground() { url url = null; try { url = new url("url in here"); bufferedinputstream bis = new bufferedinputstream(url.openstream()); byte[] buffer = new byte[1024]; stringbuilder sb = new stringbuilder(); int bytesread = 0; while((bytesread = bis.read(buffer)) > 0) { string text = new string(buffer, 0, bytesread); sb.append(text); } bis.close(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void doinbackground(void... arg0) { // todo auto-generated method stub return null; } }
i have permission internet in manifest file. again help.
here errors displayed in logcat.
07-11 11:26:31.110: e/androidruntime(9259): fatal exception: main 07-11 11:26:31.110: e/androidruntime(9259): java.lang.runtimeexception: unable start activity componentinfo{com.example.annarbormap/com.example.annarbormap.mapactivity}: android.os.networkonmainthreadexception 07-11 11:26:31.110: e/androidruntime(9259): @ android.app.activitythread.performlaunchactivity(activitythread.java:2246) 07-11 11:26:31.110: e/androidruntime(9259): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2296) 07-11 11:26:31.110: e/androidruntime(9259): @ android.app.activitythread.access$700(activitythread.java:151) 07-11 11:26:31.110: e/androidruntime(9259): @ android.app.activitythread$h.handlemessage(activitythread.java:1281) 07-11 11:26:31.110: e/androidruntime(9259): @ android.os.handler.dispatchmessage(handler.java:99) 07-11 11:26:31.110: e/androidruntime(9259): @ android.os.looper.loop(looper.java:137) 07-11 11:26:31.110: e/androidruntime(9259): @ android.app.activitythread.main(activitythread.java:5293) 07-11 11:26:31.110: e/androidruntime(9259): @ java.lang.reflect.method.invokenative(native method) 07-11 11:26:31.110: e/androidruntime(9259): @ java.lang.reflect.method.invoke(method.java:511) 07-11 11:26:31.110: e/androidruntime(9259): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 07-11 11:26:31.110: e/androidruntime(9259): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 07-11 11:26:31.110: e/androidruntime(9259): @ dalvik.system.nativestart.main(native method) 07-11 11:26:31.110: e/androidruntime(9259): caused by: android.os.networkonmainthreadexception 07-11 11:26:31.110: e/androidruntime(9259): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1118) 07-11 11:26:31.110: e/androidruntime(9259): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 07-11 11:26:31.110: e/androidruntime(9259): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 07-11 11:26:31.110: e/androidruntime(9259): @ java.net.inetaddress.getallbyname(inetaddress.java:214) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpconnection.(httpconnection.java:70) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpconnection.(httpconnection.java:50) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpconnection$address.connect(httpconnection.java:340) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpconnectionpool.get(httpconnectionpool.java:87) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpconnection.connect(httpconnection.java:128) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpengine.opensocketconnection(httpengine.java:316) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpsurlconnectionimpl$httpsengine.makesslconnection(httpsurlconnectionimpl.java:461) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpsurlconnectionimpl$httpsengine.connect(httpsurlconnectionimpl.java:433) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpengine.sendsocketrequest(httpengine.java:290) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpengine.sendrequest(httpengine.java:240) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:282) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:177) 07-11 11:26:31.110: e/androidruntime(9259): @ libcore.net.http.httpsurlconnectionimpl.getinputstream(httpsurlconnectionimpl.java:271) 07-11 11:26:31.110: e/androidruntime(9259): @ java.net.url.openstream(url.java:462) 07-11 11:26:31.110: e/androidruntime(9259): @ com.example.annarbormap.mapactivity$myasynctask.doinbackground(mapactivity.java:77) 07-11 11:26:31.110: e/androidruntime(9259): @ com.example.annarbormap.mapactivity.oncreate(mapactivity.java:48) 07-11 11:26:31.110: e/androidruntime(9259): @ android.app.activity.performcreate(activity.java:5250) 07-11 11:26:31.110: e/androidruntime(9259): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1097) 07-11 11:26:31.110: e/androidruntime(9259): @ android.app.activitythread.performlaunchactivity(activitythread.java:2210) 07-11 11:26:31.110: e/androidruntime(9259): ... 11 more
networkonmainthreadexception
you created own doinbackground methods, internet connection must in background, put code overrided methods doinbackground! see example below:
in background implement getting data, in method oppostexecute - settings data views.
private class myasynctask extends asynctask<void, void, void>{ @override protected void doinbackground(void... arg0) { url url = null; try { url = new url("url in here"); bufferedinputstream bis = new bufferedinputstream(url.openstream()); byte[] buffer = new byte[1024]; stringbuilder sb = new stringbuilder(); int bytesread = 0; while((bytesread = bis.read(buffer)) > 0) { string text = new string(buffer, 0, bytesread); sb.append(text); } bis.close(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return yourresult; } }
or
private class myasynctask extends asynctask<void, void, void>{ @override protected void doinbackground(void... arg0) { doinbackground(); //your methods return null; } protected void doinbackground() { url url = null; try { url = new url("url in here"); bufferedinputstream bis = new bufferedinputstream(url.openstream()); byte[] buffer = new byte[1024]; stringbuilder sb = new stringbuilder(); int bytesread = 0; while((bytesread = bis.read(buffer)) > 0) { string text = new string(buffer, 0, bytesread); sb.append(text); } bis.close(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
}
hope, you.
Comments
Post a Comment