gpt4 book ai didi

java - 应用程序在 android 4.0.3 上使用 url.openStream() 崩溃

转载 作者:行者123 更新时间:2023-11-29 03:34:16 24 4
gpt4 key购买 nike

 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

最佳答案

这可能是由于在 Honeycomb 中引入的 NetworkOnMainThreadException 异常。要解决此问题,您应该在单独的线程中执行所有与网络相关的工作(为了方便,您可以使用 AsyncTask)。

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

关于java - 应用程序在 android 4.0.3 上使用 url.openStream() 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16379434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com