requestRouteToHost IP参数 [英] requestRouteToHost IP argument
本文介绍了requestRouteToHost IP参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有关检查设备的网络连接我用requestRouteToHost功能,但它的第二个参数具有整数类型 - 我已经把它?难道诠释视知识产权如[1]功能使?我认为这是通过使用默认的Android功能更简单的方法。谁能告诉我?
For checking internet access on device I use requestRouteToHost function, but it's second argument has integer type - what i has to put in it? Is it int-view of IP like [1] function makes? I think there is more simple way by using default android functions. Can somebody tell me?
[1] - >
public static Long ipToInt(String addr) {
String[] addrArray = addr.split("\\.");
long num = 0;
for (int i=0;i<addrArray.length;i++) {
int power = 3-i;
num += ((Integer.parseInt(addrArray[i])%256 * Math.pow(256,power)));
}
return num;
}
感谢。
推荐答案
虽然在Android的方法来转换字符串
IP地址转换成一个int,它不是该SDK的一部分。这里的实现:
While there is a method in Android to convert a String
IP address into an int, it is not part of the SDK. Here's the implementation:
public static int lookupHost(String hostname) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
return -1;
}
byte[] addrBytes;
int addr;
addrBytes = inetAddress.getAddress();
addr = ((addrBytes[3] & 0xff) << 24)
| ((addrBytes[2] & 0xff) << 16)
| ((addrBytes[1] & 0xff) << 8)
| (addrBytes[0] & 0xff);
return addr;
}
这篇关于requestRouteToHost IP参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文