Android: UnknownHostException

AndroidHttpPostAndroid Emulator

Android Problem Overview


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 );
}

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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>

Solution 5 - Android

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

Solution 6 - Android

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.

Solution 7 - Android

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.

Solution 8 - Android

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.

Solution 9 - Android

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.

Solution 10 - Android

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

Solution 11 - Android

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

Solution 12 - Android

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.

Solution 13 - Android

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

Solution 14 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionanisbetView Question on Stackoverflow
Solution 1 - AndroidanisbetView Answer on Stackoverflow
Solution 2 - AndroidFrank VView Answer on Stackoverflow
Solution 3 - AndroidigoView Answer on Stackoverflow
Solution 4 - AndroidMoritzView Answer on Stackoverflow
Solution 5 - Androidjava devView Answer on Stackoverflow
Solution 6 - AndroiddiadyneView Answer on Stackoverflow
Solution 7 - Androidmetric152View Answer on Stackoverflow
Solution 8 - AndroidVijayView Answer on Stackoverflow
Solution 9 - AndroidtlunterView Answer on Stackoverflow
Solution 10 - AndroidTejaswi YerukalapudiView Answer on Stackoverflow
Solution 11 - Androidl33tView Answer on Stackoverflow
Solution 12 - AndroidSaravanaView Answer on Stackoverflow
Solution 13 - AndroidFrancis ShiView Answer on Stackoverflow
Solution 14 - AndroidSanaView Answer on Stackoverflow