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)
{
}
当我试图解析android 4.0.3 url.openStream()上的json响应时,它崩溃了。但是,在2.3.3上它是有效的。
堆栈跟踪
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 with 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 to start activity ComponentInfo{com.example.myvk/com.example.myvk.WallActivity}: android.os.NetworkOnMainThreadException
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.os.Looper.loop(Looper.java:137)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-05 02:43:07.580: E/AndroidRuntime(2172): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 02:43:07.580: E/AndroidRuntime(2172): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 02:43:07.580: E/AndroidRuntime(2172): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
05-05 02:43:07.580: E/AndroidRuntime(2172): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
05-05 02:43:07.580: E/AndroidRuntime(2172): at 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): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1105)
05-05 02:43:07.580: E/AndroidRuntime(2172): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-05 02:43:07.580: E/AndroidRuntime(2172): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-05 02:43:07.580: E/AndroidRuntime(2172): at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
05-05 02:43:07.580: E/AndroidRuntime(2172): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
05-05 02:43:07.580: E/AndroidRuntime(2172): at com.example.myvk.WallActivity.loadProfile(WallActivity.java:73)
05-05 02:43:07.580: E/AndroidRuntime(2172): at com.example.myvk.WallActivity.onCreate(WallActivity.java:33)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.app.Activity.performCreate(Activity.java:4465)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-05 02:43:07.580: E/AndroidRuntime(2172): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-05 02:43:07.580: E/AndroidRuntime(2172): ... 11 more
答案 0 :(得分:4)
这可能是由于Honeycomb引入的NetworkOnMainThreadException
异常。为了解决这个问题,你应该在一个单独的线程中完成你所有的网络工作(你可以使用AsyncTask
进行便利)。
有关详细信息,请阅读此博文:http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html