1

我在 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)

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

4

1 回答 1

0

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

一旦您正确设置并在后台线程上运行(我建议使用 AsyncTask 来简化过程),错误应该会消失。

于 2013-04-16T14:49:38.760 回答