简体   繁体   中英

Android socket throws UnknownHostException error

Why do I get this error...

java.net.UnknownHostException: http://google.com

...when I do this in my Activity -> onCreate?

try {
   Socket socket = new Socket("http://google.com", 80);
} catch(Exception e) {
   Log.e(tag, e.toString());
   return;
}

And yes, I do have the Internet permission set in my manifest.

<uses-permission android:name="android.permission.INTERNET" />

This is being tested on a physical Nexus S phone

使用www.google.com ,而不使用http://部分。

Is it throwing an UnknownException or UnknownHostException?

UnknownHostException means there is a problem with the hostname lookup. Try it without the "http://" and if that doesn't work, try it with the direct IP address.

Its not your app permissions that is failing, otherwise it would throw a SecurityException.

Socket throws an UnknownHostException - if the IP address of the host could not be determined.

Do you have Internet Access enabled? Try it with another host or with the IP and/or try restarting your phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM