-->

android httprequest java.net.UnknownHostException

2020-08-12 06:04发布

问题:

I want to make a http request with android.

is use this:

void testHTTP()
 {
  HttpClient httpClient = new DefaultHttpClient();

  HttpUriRequest request = new HttpPost("http://www.google.com");

  try {
   HttpResponse response = httpClient.execute(request);

   //System.out.println("response protocol version: " + response.getProtocolVersion());
  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

I have the internet permission set in my manifest file.

I keep getting java.net.UnknownHostException: www.google.com

My final purpose ios to be able to invoke a web service. So, I initially tried the ksoap2 library and the proper url and got the same result.

I suspect I have some bad setting either in my app or in my emulator.

can you give me some pointers on where to look?

it would also bee useful a working example of something similar. then i could check and if the problem persists i would know that is my development setup at fault.

btw, i am using mac snow leopard with eclipse and android 2.2 as a setup

thanks

regards

回答1:

I've had problems with the emulator where the network icon shows that I have no service, and I can't connect to the Internet. Usually, restarting the emulator fixed it. Once, I had to create a new emulator device.

Do check for clues in the emulator/device logs. You can view them in Eclipse using the LogCat view in the DDMS perspective.



回答2:

Check the emulator log messages. There's a good chance you just aren't asking for the INTERNET permission in your app manifest.



回答3:

BenTobin's answer pointed me to something I was missing. I had restarted the emulator several times to no avail, but noticed the emulator's Wi-fi indicator was showing 0 bars. I went into settings, turned Wi-fi mode off and then on and also Airplane Mode On then off. This resolved it for me. My bars came back.



回答4:

Delete you AVD recreate new AVD and restart the eclipse



回答5:

If you have permission to access the Internet is at the manifest file and the WiFi is connected so can have problems sharing the internet from your computer via WiFi, Ethernet. Try restarting internet sharing on computer. Can then will be fixed.



回答6:

Restart your wifi connection if you are testing on device, its work for me.