InetAddress.getByName on Android

I do a: serverAddr;
try {
    serverAddr =;
catch ( exception) {
    //System.err.println ("wrong server name !!!");"wrong server name !!!");

in my android application, but it's never resoling the hostname, it always throws an exception, no matter what name I use.

But using the internet on the same emulator works, and I've added

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

to AndoidManifest.xml

and here's the server class for those who assume I have none

public class Server
    public static String SERVERNAME = "";
    public static String SERVERIP = "";
    public static int SERVERPORT = 27950;
    public static int PROTOCOL = 68;

Asked by: Marcus767 | Posted: 24-01-2022

Answer 1

I was having the similar issue and I found out that in some versions of android (from honeycombs) it's not allowed by default to perform network operation from main thread. So you can resolve it in 2 ways. Perform operation in different thread or allow to make network operation in main thread. To do that use something like this:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();

Answered by: Rebecca927 | Posted: 25-02-2022

Answer 2

I've found the answer. For whatever reason, you have to use:[] x =;"Address: "+x[0].getHostAddress());

Answered by: Oliver456 | Posted: 25-02-2022

Answer 3

It is strange that you have to do so. works for me, out of the box.

There are some (on-going) issues related to DNS resolution in the Android emulator, so that might be it.

Answered by: Ryan422 | Posted: 25-02-2022

Answer 4

Don't know if it was a typo, but you said you have:

<use-permission id="android.permission.INTERNET" />

But it have to be:

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

I tried getByName and it works fine.

May be you fixed your permissions and switched from getByName to getAllByName at the same time? Just curious, if you can confirm that getByName still does not work for you?

Answered by: Brooke191 | Posted: 25-02-2022

Answer 5

At the beginning, try to resolve dns-name of local PC from the default browser (Google Chrome). If he can't - trouble in the router settings or abilities...

Answered by: Ryan609 | Posted: 25-02-2022

Similar questions

android - Why InetAddress.getByName().isReachable() works on device but not on the Eclipse AVD?

I test if device is up in the network with simple piece of code: ip_addr=""; isAvailable = InetAddress.getByName(ip_addr).isReachable(2000); That code will always return return false on my Eclipse AVD even despite the ip_addr is connectable and the subsequent request to it with http will succeed. On the other hand if the app is installed on the accual android tablet it w...

android - Java InetAddress.getByName.isReachable() Alternative

I've using InetAddress.getByName.isReachable() for a while in my app in order to know if the mobile devices can reach the server. However this need permissions in Windows 7 and I would like to know if there is an alternative of doing this without the need of adding permissions on Windows 7 firewall. Here is how I use it: boolean isReachable = false; try{ isReachable = InetAddress.getByName("10.0...

android - InetAddress.getByName () adds / to the ip

I'm having a weird problem. I'm creating a socket and giving it the IP When I use InetAddress.getByName(IP) it adds / to the ip as shown in the log below. Why this is happening ?? here is my code public class ServerCom extends AsyncTask&lt;String, Void , String&gt;{ private int port=9999; private String IP=""; private BufferedReader input; private Socket socket; priva...

java - Use String variable in InetAddress.getByName() in Android

In my Android app, I want to connect to an IP using ipaddr = InetAddress.getByName(""); Now,the issue is that this IP keeps changing due to DHCP and because the device needs to connect to different routers on different occasions. So, I added a textbox dialog at the beginning to allow the user to enter the IP manually. AlertDialog.Builder alert = new AlertDialog.Builder(th...

Why the UnknownHostException happened in Android by using InetAddress.getByName?

I am developing in Android , and I want to get the IP address via domain name. I have a WiFi device , the WiFi device join to the WiFi network same as my computer. And the domain name of WiFi device is "abc.local" , I can ping "abc.local" success via my computer. But It got the Unable to resolve host "abc.local": No address associated wi...

java - InetAddress.getByName fails getaddrinfo works

I've been having an issue with DNS lookup for IPv6 addresses that I can't seem to find good information about. I'm trying to lookup an IPv6 address using InetAddress.getByName(""). It throws an UnknownHostException error. The weird part is I know the DNS server can be contacted because this works: InetAddress.getByName("") ...

Android C code getaddrinfo takes a long time,but JAVA code InetAddress.getByName() is very fast

C code getaddrinfo(hostname, servname, hints, res); JAVA code Parse the same domain name,the C takes seconds time,but JAVA is very fast.

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android