发现Android手机互联网IP地址编程,无需设备的IP地址 [英] find internet ip address in android mobile programmatically , no need device ip address

查看:139
本文介绍了发现Android手机互联网IP地址编程,无需设备的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让互联网IP地址在我的手机。不需要设备的IP地址。该设备与WiFi或3G或2G还是反正相连。移动是有互联网设施是指,我需要一个Internet IP地址。

i am trying to get the Internet Ip Address in my mobile. no need for device ip address. that device is connected with wifi or 3g or 2g or anyway. that mobile is having internet facility means, i need that internet IP Address.

请注意:已经我尝试了一些编码。即code将努力只能获得设备的IP地址。

Note : already i tried some coding. that code will work to get the device ip address only.

我的$ P $光伏code:

my prev code :

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("ServerActivity", ex.toString());
  }

Another one code :

 WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    Log.v("hari", "ipAddress:"+ipAddress);

感谢提前
请任何一个帮助我..

Thanks Advance please any one help me..

推荐答案

您可以使用本网站和他们的API来得到它:
http://www.externalip.net/

You can use this website and their API to get it: http://www.externalip.net/

具体地
http://api.externalip.net/ip

或者(因为有人在评论中提到的),你可以在服务器上创建一个PHP文件:

Alternatively (as mentioned by someone in the comments) you could create a PHP file on your server:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

使用externalip.net URL(改编自的这个答案)

HttpGet httppost = new HttpGet("http://api.externalip.net/ip");
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String ipaddress = r.readLine();

这篇关于发现Android手机互联网IP地址编程,无需设备的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆