I've been working on project that uses the Hostname of the device connected to a local network and resolve it into ip address.But the problem is android cannot resolve the hostname of the device connected to the same local network
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread( ()->{
try {
System.out.println(InetAddress.getByName("LAPJA").getHostAddress());.
} catch (UnknownHostException e) {
System.out.println("Cannot find the specified host");
e.printStackTrace();
}
}).start();
output:
W/System.err: java.net.UnknownHostException: Unable to resolve host "LAPJA": No address associated with hostname
W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:156)
W/System.err: at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
W/System.err: at java.net.InetAddress.getByName(InetAddress.java:1106)
W/System.err: at com.example.hostname.MainActivity.lambda$onCreate$0(MainActivity.java:42)
W/System.err: at com.example.hostname.MainActivity$$ExternalSyntheticLambda0.run(Unknown Source:0)
W/System.err: at java.lang.Thread.run(Thread.java:923)
W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W/System.err: at libcore.io.Linux.android_getaddrinfo(Native Method)
W/System.err: at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:73)
W/System.err: at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:202)
W/System.err: at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:73)
W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)
W/System.err: ... 5 more
The same program works well on laptops but throws error in when ran in android (physical device not emulator)
note: Android manifest contains
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />