可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 );
}
回答1:
Ok, I feel pretty lame... The INTERNET permission tag is a child of the manifest tag, not the application tag. Sheesh!
回答2:
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.
Best of Luck!
回答3:
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.
回答4:
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>
回答5:
It happens sometimes when you are running app in the emulator. Just restart the emulator will solve the problem. It worked for me !
回答6:
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.
回答7:
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.
回答8:
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.
回答9:
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.
回答10:
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
回答11:
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
回答12:
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.
回答13:
If you open a VPN, "Unknown host exception" may be the result
回答14:
I ran into same problem when using emulator, because I changed wifi on my laptop so restarting the wifi of emulator solved my problem.