Android: UnknownHostException
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 );
}
The INTERNET permission tag is a child of the manifest tag, not the application tag.
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.