markdowneide 2013-09-17 07:32 采纳率: 0%
浏览 932

如何找到本地IP地址?

我想创建一个程序来检测网络中所有的ip4地址。
代码如下:

try {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while(e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration ee = n.getInetAddresses();

        while(ee.hasMoreElements()) {
            InetAddress i = (InetAddress) ee.nextElement();
            System.out.println(i.getHostAddress());
        }
    }
}
catch (Exception ex) {
}

在台式机上这段代码能很好的运行,但是当我使用android adk时,也是基于java就会得到null值。
请问这是什么错误呢?

try{
   Enumeration e = NetworkInterface.getNetworkInterfaces();
   while(e.hasMoreElements()){
       NetworkInterface n = (NetworkInterface) e.nextElement();
       Enumeration ee = n.getInetAddresses();
       int ii = 0;
       while(ee.hasMoreElements()){
           InetAddress i = (InetAddress) ee.nextElement();
           ips[ii] = i.getHostAddress();
           ii++;
        }
   }
} catch (Exception ex) {
}
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-21 01:54
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果