gpt4 book ai didi

Android http连接异常

转载 作者:IT老高 更新时间:2023-10-28 23:26:26 25 4
gpt4 key购买 nike

我的 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 Designing for Responsiveness.

Also see StrictMode.

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

因为诸如查找主机名之类的事情可能会花费很长时间并且有点不确定,所以不应从主线程上需要快速返回的事件函数调用它们(例如在几毫秒内)。如果其中一个函数的返回时间过长,Android 将失去向程序发送消息的能力,并且可能会弹出可怕的 Application Not Responding 对话框。

您应该将您的网络操作(至少任何需要等待结果或清除阻塞以便您发送更多的操作)转移到一个线程。如果网络操作来自一段 API 代码,您可能不应该在主线程上调用它。

关于Android http连接异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com