Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ); } 
like image 223
anisbet Avatar asked Jul 22 '10 22:07

anisbet


People also ask

How do I fix UnknownHostException?

A few tips to prevent the exception are: Double-check the hostname: Make sure there is no typo, and trim all whitespaces. Check the system's DNS settings: Make sure the DNS server is up and reachable, and if the hostname is new, wait for the DNS server to catch up.

What causes Java net UnknownHostException?

What Causes UnknownHostException. The UnknownHostException occurs when trying to connect to a remote host using its hostname, but the IP address of the host could not be determined. This usually happens because of a typo in the hostname, or because of a DNS misconfiguration or propagation delay.


1 Answers

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

like image 126
anisbet Avatar answered Nov 07 '22 05:11

anisbet