Find IP Address of Android Device with Web Service
I have installed a web service in my android device. Now I want to send a request to Android from PC via WiFi. I need the ip address of my android device to access it from a computer on the same network. How can I find the IP through my code?
Can anyone help me?
Thanks in advance.
+3
source to share
2 answers
To get the IP address of a device, use this method:
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
if this method returns null, there is no connection. If the method returns a string, this string contains the ip address currently used by the device regardless of 3G or WiFi.
+5
source to share