【发布时间】:2013-04-14 04:24:09
【问题描述】:
我拿起了我的一些旧代码进行重建,而曾经在我的平板电脑和手机上工作的方法之一不再有效。我正在尝试使用我在此处获取的例程来获取我的 IP 地址。
当程序执行第 4 行时(以“for (Enumeration...”开头),它立即跳转到异常捕获部分。异常没有消息,对于字符串 ex.getMessage() 返回 null。异常编号是 830034796568(十进制)。
这是代码。如前所述,这在一年前工作得很好。
感谢您的帮助!
public String getLocalIpAddress() {
String address= null;
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();
address = new String(inetAddress.getHostAddress().toString());
if (!inetAddress.isLoopbackAddress() && address.length() < 18) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
String msg = ex.getMessage();
//do nothing
}
return null;
}
【问题讨论】:
-
你能否将 SocketException 包装在 RuntimeException 中并重新抛出它,以便我们可以看到完整的堆栈跟踪?
-
我将其更改为“throw RuntimeException(ex); Eclipse 调试器显示的堆栈跟踪为空。我对此有点陌生并且生锈。有什么我可以做的吗?有助于他人帮助我的信息?
-
我允许 android.permission.INTERNET,但错过了 android.permission.ACCESS_NETWORK_STATE。
标签: android ip-address