Android http连接异常 - 代码日志

Android http连接异常

我的Android 3.1仿真器有一些问题。我无法获取我的XML文件。

protected InputStream getInputStream() {
    try {

        return feedUrl.openConnection().getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

这是错误跟踪:

08-07 22:34:26.657: ERROR/AndroidRuntime(563): Caused by: android.os.NetworkOnMainThreadException
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at java.net.InetAddress.getAllByName(InetAddress.java:249)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at maps.test.BaseFeedParser.getInputStream(BaseFeedParser.java:30)
08-07 22:34:26.657: ERROR/AndroidRuntime(563):     at maps.test.DomFeedParser.parse(DomFeedParser.java:23)

我认为我无法使用< uses-permission android:name =“android.permission.INTERNET”/>连接到互联网。但与此同时,Google API的工作效果非常好。

Caused by: android.os.NetworkOnMainThreadException

在Honeycomb,他们已经走了,陷入陷阱,抓住人们试图在主线程上做潜在的耗时的网络操作。

来自:http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it’s heavily discouraged. See the document 07001.

Also see 07002.

(请注意,您可以通过对您发布的错误消息的顶部的异常进行网络搜索,立即找到此信息。)

因为查找主机名的事情可能需要很长的时间,不应该从主线程上的事件函数调用,这些函数需要快速返回(如在几毫秒内)。如果这些功能之一需要太长时间才能返回,Android将失去向程序发送消息的能力,并可能弹出可怕的“应用程序不响应”对话框。

您应该移动您的网络操作(至少需要等待结果或阻塞以清除所有您可以发送更多)的线程。如果网络操作来自一个API代码,那么你可能不应该在主线程上调用它。

http://stackoverflow.com/questions/6976317/android-http-connection-exception

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:Android http连接异常