我看到该方法已被弃用,应该替换为getHostAddress()。
我的问题是,getHostAddress是如何替代的?我似乎不能让它做任何接近相同的事情。
我要做的是获取子网掩码的整数表示形式,并将其转换为字符串。
formatIPAddress完美地做到了这一点。
例如,我的子网掩码是"255.255.255.192“。WifiManager返回的整数值是105696409。formatIPAddress会正确返回此消息。
我似乎无法让getHostAddress正常工作,更不用说将整数值转换为子网掩码字符串了。
可以工作的示例代码
WifiManager wm = (WifiManager) MasterController.maincontext.getSystemService(Context.WIFI_SERVICE);
DhcpInfo wi = wm.getDhcpInfo();
int ip = wm.getDhcpInfo().ipAddress;
int gateway = wm.getDhcpInfo().gateway;
int mask = wm.getDhcpInfo().netmask;
String maskk = Formatter.formatIpAddress(mask);有谁有这方面的经验吗?我可以从格式化程序类中获取源代码并使用它。但我只想使用新的方法。
发布于 2013-06-12 09:32:54
您必须将int转换为byte[],然后使用该数组实例化InetAddress:
...
int ipAddressInt = wm.getDhcpInfo().netmask;
byte[] ipAddress = BigInteger.valueOf(ipAddressInt).toByteArray();
InetAddress myaddr = InetAddress.getByAddress(ipAddress);
String hostaddr = myaddr.getHostAddress(); // numeric representation (such as "127.0.0.1")现在我看到格式化程序需要小端字符,而bigInteger.toByteArray()返回大端字符表示,所以byte[]应该颠倒过来。
发布于 2018-03-30 11:41:40
您可以使用String.format为每个八位字节创建一个字节掩码:
...
int ipAddress = wm.getDhcpInfo().netmask;
String addressAsString = String.format(Locale.US, "%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));https://stackoverflow.com/questions/17055946
复制相似问题