【发布时间】:2012-03-31 14:54:10
【问题描述】:
我已经在我的安卓设备中设置了一个网络服务。现在我想通过WiFi从PC向android发送请求。我需要我的 android 设备的 ip 地址才能从同一网络中的 pc 访问它。如何通过我的代码找到 IP?
谁能帮帮我?
提前谢谢..
【问题讨论】:
标签: java android web-services network-protocols
我已经在我的安卓设备中设置了一个网络服务。现在我想通过WiFi从PC向android发送请求。我需要我的 android 设备的 ip 地址才能从同一网络中的 pc 访问它。如何通过我的代码找到 IP?
谁能帮帮我?
提前谢谢..
【问题讨论】:
标签: java android web-services network-protocols
获取设备ip地址使用此方法:
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无关。
【讨论】:
刚刚找到如何获取您的内部 IP: 设置 >> 无线控制 >> Wi-Fi 设置
在底部的“Wi-Fi 网络”下 点按您连接的连接
它会弹出一个窗口,其中包含如下信息:
Status
Speed
Signal Strength
Security
**IP Address**
【讨论】: