Reputation: 41
I try to get internet dynamic IP address with the following code when mobile connection is provided. getHostAddress
return 10.13.x.x ssid internal host address. But I want to get as 178.240.x.x dynamic internet IP address. Thanks in advance.
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
networkIpAdress = inetAddress.getHostAddress().toString();
}
}
Upvotes: 0
Views: 231
Reputation: 451
Use this URL to get your public IP address:
https://ident.me
Note: You might have both IPv4 and IPv6 addresses, in which case you can use https://v4.ident.me and https://v6.ident.me respectively.
The documentation is at https://api.ident.me.
Upvotes: 2
Reputation: 41
I resolved my question with following code.
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
HttpResponse response;
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
entity.getContentLength();
JSONObject json_data = new JSONObject(EntityUtils.toString(entity));
String networkIpAdress = json_data.getString("ip");
Upvotes: 0