java - App crashes with url.openStream() on android 4.0.3 -


 try{          string stringurl = ""; ///some url          url url = new url(stringurl);          reader reader = new inputstreamreader(url.openstream());          user = new gson().fromjson(reader, user.class);         }      catch (malformedurlexception e)         {          }         catch (ioexception e)         {          } 

when tried parse json response on android 4.0.3 url.openstream() crashes. but, on 2.3.3 it's works.

stacktrace

05-05 02:43:07.259: d/websettings(2172): mdatapath: /data/data/com.example.myvk 05-05 02:43:07.560: w/dalvikvm(2172): threadid=1: thread exiting uncaught exception (group=0x40c661f8) 05-05 02:43:07.580: e/androidruntime(2172): fatal exception: main 05-05 02:43:07.580: e/androidruntime(2172): java.lang.runtimeexception: unable start activity componentinfo{com.example.myvk/com.example.myvk.wallactivity}: android.os.networkonmainthreadexception 05-05 02:43:07.580: e/androidruntime(2172):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1956) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:1981) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.app.activitythread.access$600(activitythread.java:123) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.app.activitythread$h.handlemessage(activitythread.java:1147) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.os.handler.dispatchmessage(handler.java:99) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.os.looper.loop(looper.java:137) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.app.activitythread.main(activitythread.java:4424) 05-05 02:43:07.580: e/androidruntime(2172):     @ java.lang.reflect.method.invokenative(native method) 05-05 02:43:07.580: e/androidruntime(2172):     @ java.lang.reflect.method.invoke(method.java:511) 05-05 02:43:07.580: e/androidruntime(2172):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:787) 05-05 02:43:07.580: e/androidruntime(2172):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:554) 05-05 02:43:07.580: e/androidruntime(2172):     @ dalvik.system.nativestart.main(native method) 05-05 02:43:07.580: e/androidruntime(2172): caused by: android.os.networkonmainthreadexception 05-05 02:43:07.580: e/androidruntime(2172):     @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1105) 05-05 02:43:07.580: e/androidruntime(2172):     @ java.net.inetaddress.lookuphostbyname(inetaddress.java:391) 05-05 02:43:07.580: e/androidruntime(2172):     @ java.net.inetaddress.getallbynameimpl(inetaddress.java:242) 05-05 02:43:07.580: e/androidruntime(2172):     @ java.net.inetaddress.getallbyname(inetaddress.java:220) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpconnection.<init>(httpconnection.java:71) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpconnection.<init>(httpconnection.java:50) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpconnection$address.connect(httpconnection.java:351) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpconnectionpool.get(httpconnectionpool.java:86) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpconnection.connect(httpconnection.java:128) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpengine.opensocketconnection(httpengine.java:308) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpsurlconnectionimpl$httpsengine.makesslconnection(httpsurlconnectionimpl.java:460) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpsurlconnectionimpl$httpsengine.connect(httpsurlconnectionimpl.java:432) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpengine.sendsocketrequest(httpengine.java:282) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpengine.sendrequest(httpengine.java:232) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:273) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:168) 05-05 02:43:07.580: e/androidruntime(2172):     @ libcore.net.http.httpsurlconnectionimpl.getinputstream(httpsurlconnectionimpl.java:270) 05-05 02:43:07.580: e/androidruntime(2172):     @ com.example.myvk.wallactivity.loadprofile(wallactivity.java:73) 05-05 02:43:07.580: e/androidruntime(2172):     @ com.example.myvk.wallactivity.oncreate(wallactivity.java:33) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.app.activity.performcreate(activity.java:4465) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 05-05 02:43:07.580: e/androidruntime(2172):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1920) 05-05 02:43:07.580: e/androidruntime(2172):     ... 11 more 

this due networkonmainthreadexception exception introduced in honeycomb. fix should network-y stuff in seperate thread (you can use asynctask convienence).

for more information can read blog post: http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -