URLConnection doesn't work since API 10 and higher?

隐身守侯 提交于 2019-12-24 02:54:10

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/7823534/urlconnection-doesnt-work-since-api-10-and-higher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!