android httprequest java.net.UnknownHostException

2020-08-12 06:10发布

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

6条回答
我欲成王,谁敢阻挡
2楼-- · 2020-08-12 06:28

Delete you AVD recreate new AVD and restart the eclipse

查看更多
等我变得足够好
3楼-- · 2020-08-12 06:31

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.

查看更多
We Are One
4楼-- · 2020-08-12 06:31

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

查看更多
该账号已被封号
5楼-- · 2020-08-12 06:33

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.

查看更多
贪生不怕死
6楼-- · 2020-08-12 06:38

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

查看更多
Deceive 欺骗
7楼-- · 2020-08-12 06:40

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.

查看更多
登录 后发表回答