'Get ip address (internet dynamic ip)
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();
}
}
Solution 1:[1]
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.
Solution 2:[2]
If your device is connected with wifi then
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
An Android device can both be connected via WiFi, and 3G, which obviously has different IPs. The IP address on 3G will also change every time it reconnects.
Solution 3:[3]
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");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | the Tin Man |
| Solution 2 | samsad |
| Solution 3 | lol |
