1
 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
4

1 回答 1

4

这可能是由于NetworkOnMainThreadExceptionHoneycomb 中引入的异常。要解决此问题,您应该在单独的线程中完成所有网络相关的工作(您可以使用 anAsyncTask来方便)。

有关更多信息,您可以阅读此博客文章:http ://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

于 2013-05-04T22:31:54.070 回答