Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android和java中获取设备公共ip地址_Java_Android - Fatal编程技术网

如何在android和java中获取设备公共ip地址

如何在android和java中获取设备公共ip地址,java,android,Java,Android,如何在android中获取设备的公共IP地址 请在这里找到我的代码。我正在获取设备ip地址。但我如何才能获得公共ip地址 请帮帮我 import java.util.Enumeration; public class GetDeviceIp { public GetDeviceIp() {} public static String getIpAddress() { try { Enumeration<java.net.NetworkInterface&

如何在android中获取设备的公共IP地址

请在这里找到我的代码。我正在获取设备ip地址。但我如何才能获得公共ip地址

请帮帮我

import java.util.Enumeration;

public class GetDeviceIp
{
  public GetDeviceIp() {}

  public static String getIpAddress()
  {
    try {
      Enumeration<java.net.NetworkInterface> en = 
        java.net.NetworkInterface.getNetworkInterfaces(); 
        Enumeration<java.net.InetAddress> enumIpAddr;
        for (; en.hasMoreElements(); enumIpAddr.hasMoreElements())
      {
        java.net.NetworkInterface intf = (java.net.NetworkInterface)en.nextElement();
        enumIpAddr = intf.getInetAddresses();continue;
        java.net.InetAddress inetAddress = (java.net.InetAddress)enumIpAddr.nextElement();
        if ((!inetAddress.isLoopbackAddress()) && 
          ((inetAddress instanceof java.net.Inet4Address))) {
          return inetAddress.getHostAddress();
        }
      }
    }
    catch (java.net.SocketException ex) {
      ex.printStackTrace();
      return null;
    }

  }
}
import java.util.Enumeration;
公共类GetDeviceIp
{
公共GetDeviceIp(){}
公共静态字符串getIpAddress()
{
试一试{
枚举en=
java.net.NetworkInterface.getNetworkInterfaces();
枚举枚举地址;
对于(;en.hasMoreElements();EnumipAddress.hasMoreElements())
{
java.net.NetworkInterface intf=(java.net.NetworkInterface)en.nextElement();
enumIpAddr=intf.getInetAddresses();继续;
java.net.InetAddress InetAddress=(java.net.InetAddress)enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress())&&
((inetAddress instanceof java.net.Inet4Address))){
返回inetAddress.getHostAddress();
}
}
}
catch(java.net.SocketException ex){
例如printStackTrace();
返回null;
}
}
}

最简单的方法是创建http请求,从返回该请求的服务器获取公共IP。我之所以使用它,是因为它不需要任何解析,而且可能不会比这更简单

至于代码,您需要创建另一个线程来发送http请求,这在主线程中是不可能的。提出的解决方案缺少超时,是多线程的一个很差的例子,可以实现

public static String getPublicIPAddress(){
    String value = null;
    ExecutorService es = Executors.newSingleThreadExecutor();
    Future<String> result = es.submit(new Callable<String>() {
        public String call() throws Exception {
            try {
                URL url = new URL("http://whatismyip.akamai.com/");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                try {
                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                    BufferedReader r = new BufferedReader(new InputStreamReader(in));
                    StringBuilder total = new StringBuilder();
                    String line;
                    while ((line = r.readLine()) != null) {
                        total.append(line).append('\n');
                    }
                    urlConnection.disconnect();
                    return total.toString();
                }finally {
                    urlConnection.disconnect();
                }
            }catch (IOException e){
                Log.e("Public IP: ",e.getMessage());
            }
            return null;
        }
    });
    try {
        value = result.get();
    } catch (Exception e) {
        // failed
    }
    es.shutdown();
    return value;
}
public静态字符串getPublicIPAddress(){
字符串值=null;
ExecutorService es=Executors.newSingleThreadExecutor();
未来结果=es.submit(新的可调用(){
公共字符串调用()引发异常{
试一试{
URL=新URL(“http://whatismyip.akamai.com/");
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
试一试{
InputStream in=new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r=新的BufferedReader(新的InputStreamReader(in));
StringBuilder总计=新StringBuilder();
弦线;
而((line=r.readLine())!=null){
总计.append(行).append('\n');
}
urlConnection.disconnect();
返回total.toString();
}最后{
urlConnection.disconnect();
}
}捕获(IOE异常){
Log.e(“公共IP:,e.getMessage());
}
返回null;
}
});
试一试{
value=result.get();
}捕获(例外e){
//失败
}
es.shutdown();
返回值;
}

谢谢@Myszsoda。现在我可以得到公共IP地址了。但我有一个问题,如果我使用URL URL=new URL(“);“”URL有任何问题。因为我在生产中部署了jar文件。请提供一些信息。需要任何许可证吗?据我所知,没有许可证,因为它只是网站返回IP。我高度怀疑您是否需要对“http请求”进行任何许可“。这样,你可以使用任何返回你IP的网站,尽管我找不到这样简单回复的网站。正如所说的,它不是许可的API,所以你应该自由使用它,但是你可以考虑网站不能工作的选项。