开发者

Android: UnknownHostException

开发者 https://www.devze.com 2023-01-08 17:43 出处:网络
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 UnknownHost开发者_StackOverflow社区Exception. I have placed the required permissions

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 UnknownHost开发者_StackOverflow社区Exception. 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.


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.


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>


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


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.


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.


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.


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.


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


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


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消