URLConnection的,因为10 API和更高不起作用? [英] URLConnection doesn't work since API 10 and higher?

查看:129
本文介绍了URLConnection的,因为10 API和更高不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有我的Andr​​oid应用程序的一个大问题。我目前正在更新它,我从8 API切换到API 10和code以下块不工作了...

 网​​址URL =新的URL(PingUrl);
康涅狄格州的URLConnection = url.openConnection();
Conn.setConnectTimeout(4000);
INT大小= 2048;
输入的BufferedReader =新的BufferedReader(新的InputStreamReader(Conn.getInputStream()),大小);
串线= NULL;
而((行= input.readLine())!= NULL){
    郭沫若的JSONObject =新的JSONObject(线);
    字符串的timeStamp = jo.getString(时间戳);
    //Log.i(\"Value\",timeStamp);
}
input.close();
结果=真;

据崩溃@的BufferedReader ...任何人有一个想法?

  16 10-19:46:04.814:ERROR / AndroidRuntime(2151):致命异常:主要
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):了java.lang.RuntimeException:无法启动活动ComponentInfo {be.paratel.PAC / be.p​​aratel.PAC.PACMainViewActivity}:android.os.NetworkOnMainThreadException
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.app.ActivityThread.access $ 500(ActivityThread.java:122)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1024)10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.os.Handler.dispatchMessage(Handler.java:99)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.os.Looper.loop(Looper.java:132)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.app.ActivityThread.main(ActivityThread.java:4123)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在java.lang.reflect.Method.invokeNative(本机方法)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在java.lang.reflect.Method.invoke(Method.java:491)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在dalvik.system.NativeStart.main(本机方法)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):因:android.os.NetworkOnMainThreadException
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在java.net.InetAddress.lookupHostByName(InetAddress.java:477)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在java.net.InetAddress.getAllByName(InetAddress.java:249)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在be.paratel.PAC.PACMainViewActivity.pingTheApplication(PACMainViewActivity.java:245)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在be.paratel.PAC.PACMainViewActivity.ConnectionCheck(PACMainViewActivity.java:296)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在be.paratel.PAC.PACMainViewActivity.onCreate(PACMainViewActivity.java:70)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.app.Activity.performCreate(Activity.java:4397)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
10-19 16:46:04.814:ERROR / AndroidRuntime(2151):11 ...更多


解决方案

我从来没有遇到过这个问题,但我认为堆栈跟踪告诉你什么是错的: NetworkOnMainThreadException

看起来像Android操作系统被禁止在主线程同步的HTTP请求,因为这将阻止用户界面。如此看来,从做错误的决定(好主意,我认为)prevent你。您应该使用的AsyncTask 反正网络请求。

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

解决方案

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.

这篇关于URLConnection的,因为10 API和更高不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆