Android的HTTP连接异常 [英] Android http connection exception

查看:100
本文介绍了Android的HTTP连接异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,我的Andr​​oid 3.1模拟器。我不能让我的XML文件。

 受保护的InputStream的getInputStream(){
    尝试 {

        返回feedUrl.openConnection()的getInputStream()。
    }赶上(IOException异常E){
        抛出新的RuntimeException(E);
    }
}
 

下面是错误跟踪:

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

我觉得我无法连接到互联网,甚至与&LT;使用-权限的Andr​​oid:名称=android.permission.INTERNET对/&GT; 。但在同一时间,在谷歌API是工作得很好。

解决方案
  

产生的原因:android.os.NetworkOnMainThreadException

在蜂窝他们已经走了,放在一个陷阱抓住人们试图做可能很费时网络操作的主线上。

从:<一href="http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html">http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

  

例外,当应用程序试图在其主线程进行网络操作时所引发。

     

这是只抛出针对蜂窝SDK或更高版本的应用程序。针对早期SDK版本的应用程序可以做网络上的主要事件循环线程,但它严重阻碍。查看文档设计为响应

     

另请参见 StrictMode

(请注意,你可能会立即发现这个自己做一个网络搜索上的异常,在您发布的错误信息上。)

由于像查找主机名可能需要较长的和有些不确定的时间,它们不应该从这些都要求快速返回(如在几毫秒之内)主线程对事件的函数调用。如果这些功能之一的时间太长,返回,Android的损失将消息发送到该程序的能力,并可能会弹出可怕的应用程序无响应对话框。

您应该将你的网络操作(至少任何需要等待结果或堵塞,清除这样你就可以发送更多)线程。如果网络运营是从一块API code来了,你可能不应该被调用的主线程上。

I have some problems with my Android 3.1 emulator. I can't get my XML file.

protected InputStream getInputStream() {
    try {

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

Here's the error trace:

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)

I think I can't connect to Internet , even with <uses-permission android:name="android.permission.INTERNET"/>. But in the same time, the Google API is working very well.

解决方案

Caused by: android.os.NetworkOnMainThreadException

In Honeycomb they've gone and put in a trap to catch people trying to do potentially time-consuming network operations on the main thread.

From: 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.

(Please note that you could have instantly found this yourself by doing a web search on the exception at the top of the error messages you posted.)

Because things like looking up a host name can take a long and somewhat indeterminate amount of time, they should not be called from the event functions on the main thread which are required to return quickly (as in within a few milliseconds). If one of these functions takes too long to return, Android loses the ability to send messages to the program, and may pop up the dreaded Application Not Responding dialog.

You should move your networking operations (at least any that require waiting for a result or for a blockage to clear so you can send more) to a thread. If the network operations are coming from an piece of API code, you probably shouldn't be calling that on the main thread.

这篇关于Android的HTTP连接异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆