【问题标题】:I want to get the Ip address of android Phone and how can I Contact that android phone from web server Using that Ip address to get some Information?我想获取 android 手机的 IP 地址,如何从 Web 服务器联系该 android 手机使用该 IP 地址获取一些信息?
【发布时间】:2012-06-06 08:55:03
【问题描述】:

我想获取 Android 手机的 IP 地址,为此我尝试了 InetAddress,但我在 Emulator 上获得了 IP 地址 127.0.0.1。我怎样才能得到实际的 IP 地址。 其次,我想使用来自网络服务器的 IP 地址联系该 Android 手机,以询问其位置等信息。 例如:假设Phone1 想要Phone2 的信息,然后Phone1 联系webserver,webserver 使用其保存的IP 地址联系phone2,然后phone2 将其位置响应给webserver,webserver 响应phone1。

【问题讨论】:

    标签: android


    【解决方案1】:

    获取IPV4的代码:

    private String getLocalIpAddress() {
           try {
               for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                   NetworkInterface intf = en.nextElement();
                   for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                       InetAddress in`enter code here`etAddress = enumIpAddr.nextElement();
                       if (!inetAddress.isLoopbackAddress()) {
                           if (inetAddress instanceof Inet4Address) {
                               return ((Inet4Address)inetAddress).getHostAddress().toString();
                           }
                       }
                   }
               }
           } catch (SocketException ex) {
               Log.e("ServerActivity", ex.toString());
           }
           return null;
        }
    

    【讨论】:

      【解决方案2】:

      与其让服务器询问手机的位置,不如换一种方式思考。

      创建一个将在手机上运行并定期将其位置发送到您的服务器的应用。那么当Phone 2想知道Phone 1的位置时,它会从服务器获取最新的已知位置。

      【讨论】:

      • 它将用 lat lang 填满我的数据库,我只希望设备的 IP 地址在它发生变化时更新,并且我希望在请求来自 phone1 时进行异步调用,然后必须联系 phone2位置。
      • 不一定会填满您的数据库,因为您不必存储每个位置,只存储最近的位置。
      • 谢谢,但我试图以不同的方式实现它
      【解决方案3】:

      在这里找到:http://www.droidnova.com/get-the-ip-address-of-your-device,304.html

      public String getLocalIpAddress() {
      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(LOG_TAG, ex.toString());
      }
      return null;
      }
      

      因此,如果此方法返回 null,则表示没有可用的连接。 如果该方法返回一个字符串,该字符串包含设备当前使用的ip地址,与3G或WiFi无关。

      【讨论】:

      • 我也用过这段代码,它在模拟器上给了我 10.0.2.15 的 IP 地址,当它在实际设备上运行时它会给出正确的 IP 地址。
      • 我想是的,是的。注意 NAT(路由器等)。
      • 以及如何使用该 IP 地址从 android 手机访问信息
      • 可能是这样的:helloandroid.com/tutorials/…
      • 感谢您的帮助,我必须先尝试一下
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 2012-07-15
      • 2010-09-17
      • 1970-01-01
      相关资源
      最近更新 更多