Android 我想获取安卓手机的Ip地址,如何使用该Ip地址从web服务器联系安卓手机以获取信息?
我想获得Android手机的IP地址,为此我尝试了InetAddress,但在Emulator上获得了IP地址127.0.0.1。如何获取实际的IP地址。 其次,我想通过网络服务器的IP地址联系android手机,询问一些信息,比如它的位置。Android 我想获取安卓手机的Ip地址,如何使用该Ip地址从web服务器联系安卓手机以获取信息?,android,Android,我想获得Android手机的IP地址,为此我尝试了InetAddress,但在Emulator上获得了IP地址127.0.0.1。如何获取实际的IP地址。 其次,我想通过网络服务器的IP地址联系android手机,询问一些信息,比如它的位置。 例如:假设Phone1需要Phone2的信息,然后Phone1联系Web服务器,Web服务器使用其保存的ip地址联系Phone2,然后Phone2响应其到Web服务器的位置,Web服务器响应Phone1 与其让服务器询问手机的位置,不如换一种方式思考 创建
例如:假设Phone1需要Phone2的信息,然后Phone1联系Web服务器,Web服务器使用其保存的ip地址联系Phone2,然后Phone2响应其到Web服务器的位置,Web服务器响应Phone1 与其让服务器询问手机的位置,不如换一种方式思考 创建一个应用程序,该应用程序将在手机上运行,并定期将其位置发送到您的服务器。然后,当电话2想要知道电话1的位置时,它将从服务器获取最新的已知位置 可在此处找到:
公共字符串getLocalIpAddress(){
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
返回inetAddress.getHostAddress().toString();
}
}
}
}捕获(SocketException例外){
Log.e(Log_标记,例如toString());
}
返回null;
}
因此,如果此方法返回null,则没有可用的连接。
如果该方法返回字符串,则该字符串包含设备当前使用的ip地址,与3G或WiFi无关
获取IPV4的以下代码:
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress in`enter code here`etAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
if (inetAddress instanceof Inet4Address) {
return ((Inet4Address)inetAddress).getHostAddress().toString();
}
}
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}
私有字符串getLocalIpAddress(){
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
`enter code here`etAddress=EnumipAddress.nextElement()中的InetAddress;
如果(!inetAddress.isLoopbackAddress()){
if(Inet4Address的inetAddress实例){
返回((Inet4Address)inetAddress.getHostAddress().toString();
}
}
}
}
}捕获(SocketException例外){
Log.e(“ServerActivity”,例如toString());
}
返回null;
}
它将用lat lang填充我的数据库,我只希望设备的ip地址在每次更改时都会更新,并且我希望当请求来自phone1时,只需联系phone2以获取位置。不一定要填充你的数据库,因为你不必存储每个位置,只是最近的。谢谢,但我尝试以不同的方式实现它。我使用了这段代码,它在emulator上为我提供了IP地址10.0.2.15,在实际设备上运行时,它是否会提供正确的IP地址。我想是的。注意NAT(路由器等)。我如何使用该IP地址访问android手机上的信息可能是这样的:谢谢你的帮助,我必须先试试
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress in`enter code here`etAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
if (inetAddress instanceof Inet4Address) {
return ((Inet4Address)inetAddress).getHostAddress().toString();
}
}
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}