oueslatibilel
oueslatibilel

Reputation: 567

Android http connection exception

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.

Upvotes: 19

Views: 54598

Answers (5)

ramkumar5035
ramkumar5035

Reputation: 359

Use Async Task or

if (android.os.Build.VERSION.SDK_INT > 9) 
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Upvotes: 15

sakivns
sakivns

Reputation: 55

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

I removed it and my problem solved

Upvotes: 0

Nirav Dangi
Nirav Dangi

Reputation: 3647

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.!!

Upvotes: 3

Chris Stratton
Chris Stratton

Reputation: 40337

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.

Upvotes: 54

Pikaling
Pikaling

Reputation: 8312

Do you have

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

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

Upvotes: 4

Related Questions