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 ); }
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 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.
The INTERNET permission tag is a child of the manifest tag, not the application tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With