68

I am using Android SDK 2.2, testing my application with the emulator. I want to send a HTTP Post. When I do I get a UnknownHostException. I have placed the required permissions
<uses-permission android:name="android.permission.INTERNET" />
in the manifest.xml. Also I can open the browser on the emulator and navigate to the URL with no problem.

Here is my code:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost( uri );
HttpResponse response = null;
try
{
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2 );
nameValuePairs.add( new BasicNameValuePair( "id", "edit-name" ) );
nameValuePairs
.add( new BasicNameValuePair( "stringdata", userName ) );
httppost.setEntity( new UrlEncodedFormEntity( nameValuePairs ) );

// Execute HTTP Post Request
response = httpclient.execute( httppost );
// Log.i( "HttpManager:", "======> response: "
// + response.getEntity().getContent() );

}
catch (ClientProtocolException e)
{
Log.e( "HttpManager", "ClientProtocolException thrown" + e );
}
catch (IOException e)
{
Log.e( "HttpManager", "IOException thrown" + e );
}
Walter Mundt
  • 24,753
  • 5
  • 53
  • 61
anisbet
  • 2,662
  • 2
  • 20
  • 12
  • `} catch(Exception e) { System.out.println("Error:"+e); } catch (ClientProtocolException e) { Log.e( "HttpManager", "ClientProtocolException thrown" + e ); } catch (IOException e) { Log.e( "HttpManager", "IOException thrown" + e ); }` This Shows error says as already handled exception. – Abdulla Sirajudeen Jun 25 '16 at 18:50

14 Answers14

116

The INTERNET permission tag is a child of the manifest tag, not the application tag.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
anisbet
  • 2,662
  • 2
  • 20
  • 12
51

For others' consideration, I ran in to this problem and a Google landed me. As mentioned by anisbet, I double checked my permission tag and it was in the right spot.

I eventually fired up the android built in browser and was getting the same response from my web server as well as Google.com (while the computer itself was fine). I terminated the android emulator and restarted; worked on the first try.

After reviewing your code, it may be worth while to restart the emulator. In all fairness to the emulator, a bunch of programs crashed shortly after doing this, so perhaps something else was going on in my computer. Still, this wasted a ton of time for me so perhaps this will save someone the headache I went though.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Frank V
  • 25,141
  • 34
  • 106
  • 144
  • Same works for debugging on an actual device—just restart it :) – MSpeed Nov 23 '11 at 14:01
  • The Android Emulator doesn't seem to like some network changes, even the Intel HAXM-accelerated Gingerbread one. If I move from one WiFi network to one powered by a MiFi, it gives me this error and I end up restarting the Emulator. – louielouie Jun 10 '12 at 00:19
35

Make sure you have an internet connection. That's what happened to me when I forgot I am testing with mobile phone with no internet connection.

igo
  • 6,359
  • 6
  • 42
  • 51
  • 2
    Oh wow i cant believe this was my problem! make sure wifi is on when using extra test devices guys! thanks – Glenn.nz May 16 '13 at 00:08
  • Yes, I was assuming the internet was going via the debugging cable along USB but of course, it goes directly from the phone! – Lukos Jun 17 '13 at 12:54
17

You know what solved it for me was putting the permission just before the closing manifest tag, like so:

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Moritz
  • 3,235
  • 26
  • 17
9

It happens sometimes when you are running app in the emulator. Just restart the emulator will solve the problem. It worked for me !

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
java dev
  • 1,044
  • 1
  • 11
  • 17
5

If none of the above worked, try taking a step back and making sure that your device or emulator can actually reach the internet by opening up a browser.

diadyne
  • 4,038
  • 36
  • 28
2

A final check would be that your domain name is a valid domain. Having a underscore in a domain is invalid and will throw an unknown host exception.

tlunter
  • 724
  • 1
  • 5
  • 14
  • This is a rare issue but is really annoying! See explanation [here](http://stackoverflow.com/a/11206362/465942) – Kapé Mar 24 '15 at 14:57
2

I ran into a similar problem when testing an app that had a minSdkVersion set to 4 and I was trying to run it on a G1. Changing it to 3 solved the problem for me.

metric152
  • 422
  • 4
  • 16
2

I ran in to the same issue. I have the correct permissions in my Android Manifest file and the Url is correct too. I am getting the response in the web browser. I restarted the IDE, Emulator, but didn't fix the problem. So i deleted the AVD using AVD manager and then started the emulator and it started to work.

Vijay
  • 21
  • 1
1

One other thing: It turned out that the internet itself wasn't working for me. Launching the emulator from the commandline with these switches fixed it for me: emulator -avd your_avd_name -dns-server 8.8.8.8

Tejaswi Yerukalapudi
  • 8,987
  • 12
  • 60
  • 101
0

I've seen this error when connected to WiFi. As soon as I turned off WiFi, it worked. UnknownHostException could very well be thrown due to this Android bug:

http://code.google.com/p/android/issues/detail?id=67324

l33t
  • 18,692
  • 16
  • 103
  • 180
0

Check this also if you're not using Emulator

I got the same issue today, i am not using Emulator but enabled USB debugging in mobile for testing.

I didn't turn on data in my mobile, so i got UnknownHostException, once I turn-on it got resolved.

Saravana
  • 12,647
  • 2
  • 39
  • 57
0

If you open a VPN, "Unknown host exception" may be the result

Francis Shi
  • 395
  • 4
  • 8
0

I ran into same problem when using emulator, because I changed wifi on my laptop so restarting the wifi of emulator solved my problem.

Sana
  • 49
  • 5