When I read the IP address of the device I always get the local IP address.
I use the following code snippet to do that.
public String getIpAddress() {
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()) {
String ip = Formatter.formatIpAddress(inetAddress.hashCode());
Log.d("VPNConnected",ip);
return ip;
}
}
}
} catch (Exception ex) {
Log.d("exception", ex.toString());
}
return "EMPTY";
}
But I need to read the external IP address without using any external host or web apis such as http://jsonip.com
traceroute
, you need an external host that you are trying to trace the route to. Then, you would need your own algorithms for determining what a "real world IP" is. – Bouldin