在Gingerbread和Jelly Bean上运行我的应用程序没有问题,但是在Honeycomb上运行时出现错误。 以下是创建错误的以下代码:

BufferedReader in = new BufferedReader(new InputStreamReader(
            url.openStream()));

其中url是引用公共网站上的文本的URL对象。 之后,我调用以下代码行以提取文本:

String inputLine;
String message = "";
while ((inputLine = in.readLine()) != null)
        message = message + inputLine;
in.close();

但是,它不会超出BufferedReader代码行。 调用该行代码后,出现以下错误:

04-16 08:36:50.750: W/System.err(5051): android.os.NetworkOnMainThreadException
04-16 08:36:50.750: W/System.err(5051):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1119)
04-16 08:36:50.750: W/System.err(5051):     at java.net.InetAddress.lookupHostByName(InetAddress.java:441)
04-16 08:36:50.750: W/System.err(5051):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:243)
04-16 08:36:50.750: W/System.err(5051):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
04-16 08:36:50.758: W/System.err(5051):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
04-16 08:36:50.766: W/System.err(5051):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
04-16 08:36:50.766: W/System.err(5051):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
04-16 08:36:50.766: W/System.err(5051):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
04-16 08:36:50.766: W/System.err(5051):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
04-16 08:36:50.766: W/System.err(5051):     at java.net.URL.openStream(URL.java:462)
04-16 08:36:50.766: W/System.err(5051):     at com.hasta.upark.Data.getOccupancy(Data.java:39)
04-16 08:36:50.774: W/System.err(5051):     at com.hasta.upark.MapsActivity.updateMarker(MapsActivity.java:362)
04-16 08:36:50.774: W/System.err(5051):     at com.hasta.upark.MapsActivity$2.run(MapsActivity.java:159)
04-16 08:36:50.774: W/System.err(5051):     at android.os.Handler.handleCallback(Handler.java:605)
04-16 08:36:50.774: W/System.err(5051):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 08:36:50.774: W/System.err(5051):     at android.os.Looper.loop(Looper.java:137)
04-16 08:36:50.774: W/System.err(5051):     at android.app.ActivityThread.main(ActivityThread.java:4697)
04-16 08:36:50.774: W/System.err(5051):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 08:36:50.774: W/System.err(5051):     at java.lang.reflect.Method.invoke(Method.java:511)
04-16 08:36:50.774: W/System.err(5051):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-16 08:36:50.774: W/System.err(5051):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-16 08:36:50.774: W/System.err(5051):     at dalvik.system.NativeStart.main(Native Method)

任何帮助表示赞赏,谢谢!

===============>>#1 票数:0 已采纳

错误的第一行: android.os.NetworkOnMainThreadException ...并非像您想象的那样在后台线程上发生。 仔细检查一下。

一旦正确安装它并在后台线程上运行(我建议使用AsyncTask简化该过程),该错误就会消失。

  ask by Fobos13 translate from so

未解决问题?本站智能推荐: