0

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 java.net.UnknownHostException: Unable to resolve host "abc.local": No address associated with hostname when I use the following code in Android, .

               new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            InetAddress address = InetAddress.getByName("abc.local");
                        }
                        catch(Exception e) {
                            Log.d(TAG,"mConnectionMode error = " + e);
                        }
                    }
                }).start();

I also add the following permission in AndroidMainifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Why the Android device can not get the ip Address via domain name ?

Thanks in advance.

Wun
  • 6,211
  • 11
  • 56
  • 101

1 Answers1

0

How did you set a domain name for a device?

When checking for IP adresses, the device checks with the DNS server for a resolution. I'm having a hunch that you just added abc.local to your hosts file on your computer, and that's why it is working. But that will only work on that computer.

If you could give us some more details how you have set this up, maybe we can help you.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71