Anu*_*Anu 3 java android web-services network-protocols
我在我的Android设备中设置了一个Web服务.现在我想通过WiFi从PC发送请求到android.我需要我的Android设备的IP地址从同一网络中的PC访问它.如何通过我的代码找到IP?
谁能帮我?
提前致谢..
要获取设备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;
}
Run Code Online (Sandbox Code Playgroud)
如果此方法返回null,则没有可用的连接.如果该方法返回一个字符串,则该字符串包含设备当前使用的独立于3G或WiFi的IP地址.