自 API 10 及更高版本以来 URLConnection 不起作用?

发布于 2024-12-10 22:38:51 字数 5045 浏览 0 评论 0原文

嘿,我的 Android 应用程序遇到了一个大问题。我目前正在更新它,并从 API 8 切换到 API 10,并且以下代码块不再工作......

URL url = new URL(PingUrl);
URLConnection Conn = url.openConnection();
Conn.setConnectTimeout(4000);
int Size = 2048;
BufferedReader input = new BufferedReader(new InputStreamReader(Conn.getInputStream()), Size);
String line = null;
while ((line = input.readLine()) != null) {
    JSONObject jo = new JSONObject(line);
    String timeStamp = jo.getString("timestamp");
    //Log.i("Value",timeStamp);
}
input.close();
result = true;

它崩溃了@ BufferedReader ...有人知道吗?

10-19 16:46:04.814: ERROR/AndroidRuntime(2151): FATAL EXCEPTION: main
10-19 16:46:04.814: ERROR/AndroidRuntime(2151): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.paratel.PAC/be.paratel.PAC.PACMainViewActivity}: android.os.NetworkOnMainThreadException
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)

10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.os.Looper.loop(Looper.java:132)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.main(ActivityThread.java:4123)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.lang.reflect.Method.invoke(Method.java:491)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at dalvik.system.NativeStart.main(Native Method)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151): Caused by: android.os.NetworkOnMainThreadException
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.net.InetAddress.getAllByName(InetAddress.java:249)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at be.paratel.PAC.PACMainViewActivity.pingTheApplication(PACMainViewActivity.java:245)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at be.paratel.PAC.PACMainViewActivity.ConnectionCheck(PACMainViewActivity.java:296)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at be.paratel.PAC.PACMainViewActivity.onCreate(PACMainViewActivity.java:70)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.Activity.performCreate(Activity.java:4397)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     ... 11 more

Hey I'm having a big issue with my android App. I'm currently updating it and I switched from API 8 to API 10 and the following block of code doesn't work anymore ...

URL url = new URL(PingUrl);
URLConnection Conn = url.openConnection();
Conn.setConnectTimeout(4000);
int Size = 2048;
BufferedReader input = new BufferedReader(new InputStreamReader(Conn.getInputStream()), Size);
String line = null;
while ((line = input.readLine()) != null) {
    JSONObject jo = new JSONObject(line);
    String timeStamp = jo.getString("timestamp");
    //Log.i("Value",timeStamp);
}
input.close();
result = true;

It crashes @ BufferedReader ... anyone got an idea ?

10-19 16:46:04.814: ERROR/AndroidRuntime(2151): FATAL EXCEPTION: main
10-19 16:46:04.814: ERROR/AndroidRuntime(2151): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.paratel.PAC/be.paratel.PAC.PACMainViewActivity}: android.os.NetworkOnMainThreadException
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)

10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.os.Looper.loop(Looper.java:132)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.main(ActivityThread.java:4123)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.lang.reflect.Method.invoke(Method.java:491)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at dalvik.system.NativeStart.main(Native Method)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151): Caused by: android.os.NetworkOnMainThreadException
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at java.net.InetAddress.getAllByName(InetAddress.java:249)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at be.paratel.PAC.PACMainViewActivity.pingTheApplication(PACMainViewActivity.java:245)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at be.paratel.PAC.PACMainViewActivity.ConnectionCheck(PACMainViewActivity.java:296)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at be.paratel.PAC.PACMainViewActivity.onCreate(PACMainViewActivity.java:70)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.Activity.performCreate(Activity.java:4397)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
10-19 16:46:04.814: ERROR/AndroidRuntime(2151):     ... 11 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装爱人 2024-12-17 22:38:51

我从未遇到过这个问题,但我认为堆栈跟踪会告诉您出了什么问题: NetworkOnMainThreadException

似乎 Android 操作系统禁止主线程上的同步 HTTP 请求,因为这会阻塞 UI。所以它似乎可以防止你做出错误的决定(我认为是个好主意)。无论如何,您应该对网络请求使用 AsyncTask

I never faced that problem but I think the stacktrace is telling you what is wrong: NetworkOnMainThreadException

Seems like android OS is forbidding a synchronous HTTP request on the main thread, because this will block the UI. So it seems to prevent you from making bad decisions (good idea I think). You should use an AsyncTask for network requests anyway.

悲欢浪云 2024-12-17 22:38:51

看看这篇文章,它介绍了一些 Android HTTP 客户端以及它们所经历的一些变化。

Android 的 HTTP 客户端

Take a look at this article, it features some of the android HTTP clients and some of the changes they have experienced.

Android’s HTTP Clients

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文