Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-Formatter.formatIPAddress与API 12的弃用_Android - Fatal编程技术网

Android-Formatter.formatIPAddress与API 12的弃用

Android-Formatter.formatIPAddress与API 12的弃用,android,Android,我发现该方法已被弃用,替换方法应该是getHostAddress() 我的问题是getHostAddress如何替换?我似乎不能让它做任何接近同样的事情 我要做的是获取子网掩码的整数表示形式,并将其转换为字符串 formatIPAddress可以完美地实现这一点 例如,我的子网掩码是“255.255.255.192”。WifiManager返回的整数值为105696409。formatIPAddress正确返回此值 我似乎无法让getHostAddress正常工作,更不用说将整数值转换为子网掩码

我发现该方法已被弃用,替换方法应该是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);

有人有这方面的经验吗?我可以从formatter类中获取源代码并使用它。但我只想用新方法

必须将int转换为byte[],然后使用该数组转换为instance 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")

现在我看到格式化程序需要小尾数和大整数。toByteArray()返回一个大尾数表示,因此字节[]应该反转。

您可以使用
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));

我真的看不出有什么理由反对formatIpAddress。但是你到底是怎么做到的呢?你有两个不同的
ipAddress
声明,你需要重命名其中一个。:-)