Java 返回IP地址的Android函数
我正在将数据从android设备发送到本地主机数据库。为此,我需要使用我的ip地址。我可以通过在命令提示符下搜索“ipconfig”找到我的ip地址。我只是注意到我的ip地址略有变化,即使我使用相同的wifi连接。我的ip地址的最后一位数字已更改。这需要在我的代码中做一点小改动,但我想知道是否有一个android函数可以为您返回计算机的ip地址,这样代码就可以如下所示。当使用其他形式的互联网连接来更改您的ip地址时,此功能也会有所帮助Java 返回IP地址的Android函数,java,android,Java,Android,我正在将数据从android设备发送到本地主机数据库。为此,我需要使用我的ip地址。我可以通过在命令提示符下搜索“ipconfig”找到我的ip地址。我只是注意到我的ip地址略有变化,即使我使用相同的wifi连接。我的ip地址的最后一位数字已更改。这需要在我的代码中做一点小改动,但我想知道是否有一个android函数可以为您返回计算机的ip地址,这样代码就可以如下所示。当使用其他形式的互联网连接来更改您的ip地址时,此功能也会有所帮助 HttpPost httpPost = new HttpPo
HttpPost httpPost = new HttpPost("http://" + ipAddressFunction() + "/linker.php");// home wifi
首先通过以下方式获取网络接口:
NetworkInterface.getNetworkInterfaces();
然后获取IP(s)va:
您可以尝试连接多个IP,如果返回的数据被证明是计算机IP这里是我用来获取Android设备IP的函数
//Get The Local IP Addresses
private String GetLocalIPAddress(Boolean LoopBack)
{
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.getHostAddress().contains("."))
{
if(inetAddress.isLoopbackAddress() == LoopBack)
{
return inetAddress.getHostAddress();
}
}
}
}
}
catch (Exception e)
{
Log.d(this.getClass().toString(), "Failed Getting Local IP\n\n" + e.getMessage());
}
return null;
}
//获取本地IP地址
私有字符串GetLocalIPAddress(布尔环回)
{
尝试
{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();)
{
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();)
{
InetAddress InetAddress=enumIpAddr.nextElement();
如果(inetAddress.getHostAddress()包含(“.”)
{
if(inetAddress.isLoopbackAddress()==环回)
{
返回inetAddress.getHostAddress();
}
}
}
}
}
捕获(例外e)
{
Log.d(this.getClass().toString(),“获取本地IP失败\n\n”+e.getMessage());
}
返回null;
}
此函数接收一个布尔参数,如果为True,它将返回本地环回IP(即:127.0.0.1),如果为False,它将返回正常IP(即:192.168.0.5)这有帮助吗?如果你的Android手机不使用某种名称服务或某种广播服务发现方案,或者不被其他机制告知,就无法直接知道网络上另一台机器的地址。@ChrisStratton关于stackoverflow有什么例子吗?是的,这三个都可能涉及。选择一个并研究它。关闭电脑上的DHCP协议,给它一个固定的本地ip地址。在我阅读问题时,他们希望一台机器(Android设备)来确定另一台机器的ip地址(本地wifi上的某个框承载服务器-可能是windows one,给出
ipconfig
命令)。这确实返回android ip地址,但我需要一个用于我电脑的ip地址。嗯,我不知道如何从移动设备获取特定的PC ip。但是,如果只有最后的数字发生了变化,您可以设置一个扫描循环,提供一系列选项。
//Get The Local IP Addresses
private String GetLocalIPAddress(Boolean LoopBack)
{
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.getHostAddress().contains("."))
{
if(inetAddress.isLoopbackAddress() == LoopBack)
{
return inetAddress.getHostAddress();
}
}
}
}
}
catch (Exception e)
{
Log.d(this.getClass().toString(), "Failed Getting Local IP\n\n" + e.getMessage());
}
return null;
}