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.(HttpConnection.java:69)
08-07 22:34:26.657: ERROR/AndroidRuntime(563): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(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
. But in the same time, the Google API is working very well.
Answer
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.
No comments:
Post a Comment