Android 怎么获取手机端的ip地址
在和服务器互动的时候,常常会用到客户端ip地址。当然,在服务器端可以获取请求过来的ip,在手机端,怎么获取自己的ip呢?请参阅下面的函数:
[java] public static String GetHostIp() {
try {
for (Enumeration<NetworkInte易做图ce> en = NetworkInte易做图ce
.getNetworkInte易做图ces(); en.hasMoreElements();) {
NetworkInte易做图ce intf = en.nextElement();
for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
.hasMoreElements();) {
InetAddress inetAddress = ipAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
} catch (Exception e) {
}
return null;
}
public static String GetHostIp() {
try {
for (Enumeration<NetworkInte易做图ce> en = NetworkInte易做图ce
.getNetworkInte易做图ces(); en.hasMoreElements();) {
NetworkInte易做图ce intf = en.nextElement();
for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
.hasMoreElements();) {
InetAddress inetAddress = ipAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
} catch (Exception e) {
}
return null;
}
其实,上面的方法通过java.net下的相关类获取ip的。主要用到的类有:java.net.NetworkInte易做图ce和java.net.InetAddress
摘自 心灵净土
补充:移动开发 , Android ,