【问题标题】:Fetch server ip of android device获取android设备的服务器ip
【发布时间】:2012-09-10 06:36:17
【问题描述】:

在我的应用程序中,我需要获取服务器 ip.. 我获取 ip 的代码是

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();
                Log.v("","ip1--:" + inetAddress);
                Log.v("", "ip2--:" + inetAddress.getHostAddress());       
                String ipv4;
      if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {

                    String ip = inetAddress.getHostAddress().toString();
                    Toast.makeText(getApplicationContext(), inetAddress., Toast.LENGTH_SHORT).show();
                    Log.v("","ip---::" + ip);
                    // return inetAddress.getHostAddress().toString();
                    return ipv4;
                }
            }
        }

但如果设备连接到联网的 wifi 设备,它会返回本地地址。请告诉我如何获取网络的父 ip。提前谢谢...

【问题讨论】:

    标签: java android client-server ip


    【解决方案1】:
    private String wifi_ip() 
        {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet("http://wiki.iti-lab.org/ip.php");
                HttpResponse response;
    
                response = httpclient.execute(httpget);
    
                // Log.i("externalip",response.getStatusLine().toString());
    
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    long len = entity.getContentLength();
                    if (len != -1 && len < 1024) {
                        String str = EntityUtils.toString(entity);
                        // Log.i("externalip",str);
                        // ip.setText(str);
                        ip1 = str;
                    }
                }
            } catch (Exception e) {
                // ip.setText("Error");
            }
            return ip1;
        }
    

    【讨论】:

      【解决方案2】:
      WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
      WifiInfo wifiInfo = wifiManager.getConnectionInfo();
      int ipAddress = wifiInfo.getIpAddress();
      

      注意 ipAddress 是一个整数。

      例如,如果您的 IP 地址是“86.52.119.245”:

      the int returned by getIpAddress will be 4118230102.
      Translated into binary this number is: 11110101 01110111 00110100 01010110.
      Convert each byte into a decimal, and then you get the numbers: 80 35 83 10
      Notice that the numbers are in network order so you have to flip it.
      

      【讨论】:

      • 这里的函数 getIpAddress() 返回一个 9 位整数,但你写了 10 位整数。请告诉解决方案.. thnx..
      • 继续把它翻译成二进制!然后将每个字节转换为十进制!如果仍然有问题,请在此处输入 9 位数字!
      • 我有一个 30 位的二进制字符串,请告诉我将其转换为 IP 地址的方法。你已经制作了 8 组,但在我的情况下,有 2 位更少。现在该怎么办??请帮忙..
      • 我已经在函数 Integer.toBinaryString(4118230102) 中尝试了您提供的整数 4118230102;但它给出了超出范围整数的错误。我完全被困在这里,请帮助我......
      • 感谢 Aditya 的宝贵回答我的问题已经解决了我参考了这个链接stackoverflow.com/questions/7975473/…
      猜你喜欢
      • 2019-03-20
      • 2021-10-27
      • 2012-09-14
      • 2015-09-04
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多