2014-10-27 84 views
0

我尝试使用以下代码获取互联网动态IP地址,当提供移动连接时。 getHostAddress返回10.13.x.x ssid内部主机地址。但我想获得作为178.240.x.x动态互联网IP地址。感谢您的建议。获取IP地址(互联网动态IP)

for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { 
        InetAddress inetAddress = enumIpAddr.nextElement(); 
        if (!inetAddress.isLoopbackAddress()) { 
         networkIpAdress = inetAddress.getHostAddress().toString(); 
        } 
       } 
+2

您需要为此提供外部资源,如此处所述:http://stackoverflow.com/a/16781615/2413303 – EpicPandaForce 2014-10-27 09:27:07

回答

0

如果您的设备与无线网络连接,然后

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
int ipAddress = wifiInfo.getIpAddress(); 

的Android设备都可以通过WiFi进行连接,3G等,这显然有不同的IP地址。 每次重新连接时,3G上的IP地址也会发生变化。

0

我用下面的代码解决了我的问题。

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");