19

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.

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
oueslatibilel
  • 567
  • 1
  • 8
  • 24
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – laalto May 11 '13 at 16:39

5 Answers5

54

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.

Community
  • 1
  • 1
Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • i am recieving the same error but i have my http work in a seperate thread i.e. async task. what could have gone wrong? could you please guide me on this? – Nitesh Verma Feb 25 '14 at 10:51
  • You have probably not used async task correctly, and either not put all the network operations in the background method, or else called the background method from the ui thread rather than starting the async task. When you examine your stack trace you will find out which. – Chris Stratton Feb 25 '14 at 13:00
  • i checked thoroughly, the http call is called in the doInBackground() method which is called from the .execute() command, still the stream returns: org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream@405259f0 – Nitesh Verma Feb 26 '14 at 03:56
15

Use Async Task or

if (android.os.Build.VERSION.SDK_INT > 9) 
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}
Howli
  • 12,291
  • 19
  • 47
  • 72
ramkumar5035
  • 361
  • 1
  • 4
  • 11
  • This is such a great hint for all kinds of quick evaluation. You don't have to lose time with thread/async details. Thx! – peter_the_oak Sep 30 '15 at 12:41
4

Do you have

<uses-permission android:name="android.permission.INTERNET" />

in your Android Manifest? It should be above your application tag

Pikaling
  • 8,282
  • 3
  • 27
  • 44
3

This exception is only thrown for applications targeting the Honeycomb SDK or higher. you may be targeting your sdk equal to or higher than api level 11. Just change it to api level 8 or other api level below 11. your problem may be solved

Here is the same problem, it had solved by change api level.

I hope it helps you.!!

Community
  • 1
  • 1
Nirav Dangi
  • 3,607
  • 4
  • 49
  • 60
  • 2
    Actually the way I understood it, this exception is thrown on purpose to prevent time-consuming network operations on the main thread. So the problem is not the API he's targeting but the way he handles network operations ;) – AgentKnopf Jun 17 '13 at 09:06
0

Remove the android:targetSdkVersion"version_number" from your AndroidManifest.xml

I removed it and my problem solved

sakivns
  • 55
  • 6