你能解释一下 android 中 requestRouteToHost() 的功能吗?
在我的代码中,我使用 requestRouteToHost()
方法:
此路由是否意味着将 WIFI 更改为 3G,反之亦然?
我的代码不起作用...
public static boolean isHostAvailable(Context context, String urlString) throws UnknownHostException, MalformedURLException {
boolean ret = false;
int networkType = ConnectivityManager.TYPE_WIFI;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm != null){
NetworkInfo nf = cm.getActiveNetworkInfo();
if(nf != null){
networkType = nf.getType();
}
URL url = new URL(urlString);
InetAddress iAddress = InetAddress.getByName(url.getHost());
ret = cm.requestRouteToHost(networkType, ipToInt(iAddress.getHostAddress()));
}
return ret;
}
public static int ipToInt(String addr) {
String[] addrArray = addr.split("\\.");
int 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;
}
谢谢
In my code I am using requestRouteToHost()
method:
Does this routing means changing the WIFI to 3G or vice versa??
My code is not working...
public static boolean isHostAvailable(Context context, String urlString) throws UnknownHostException, MalformedURLException {
boolean ret = false;
int networkType = ConnectivityManager.TYPE_WIFI;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm != null){
NetworkInfo nf = cm.getActiveNetworkInfo();
if(nf != null){
networkType = nf.getType();
}
URL url = new URL(urlString);
InetAddress iAddress = InetAddress.getByName(url.getHost());
ret = cm.requestRouteToHost(networkType, ipToInt(iAddress.getHostAddress()));
}
return ret;
}
public static int ipToInt(String addr) {
String[] addrArray = addr.split("\\.");
int 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;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是一种记录极差的方法,虽然上面的评论说“将其视为 ping”可能是一个合理的解释,但我认为这是不正确的。事实上,它采用 int 作为主机地址,这表明它是一个比这低得多的方法,JavaDoc 中的注释
此方法要求调用者持有 CHANGE_NETWORK_STATE 权限
是另一个线索,表明这会更改设备的内部路由表。 此链接提供了更好的解释:考虑到所需的许可,这种解释更有意义。它似乎也无法与 WiFi 一起使用。因此,该方法的用途如下:您希望确保与特定主机的连接将通过特定接口进行,并且该接口不是 WiFi。这对于长期、低流量、电池高效的连接可能有意义,例如当您希望保持套接字对服务器打开并等待服务器偶尔发送消息时。移动数据接口比 WiFi 更有意义,因为您不需要一直保持 WiFi 无线电处于活动状态,并且移动网络无线电始终处于开启状态。顺便说一句,这正是 iPhone 的服务器“推送”机制的工作原理:它通过移动数据接口保持与 Apple 服务器的套接字不断连接,等待服务器发出指令。
因此,与(当前选择的)正确答案相反,我建议提问者的问题的答案:
此路由是否意味着将 WIFI 更改为 3G,反之亦然?
实际上是,“是的,有点!”如果该方法返回 true,则调用者确信将通过指定的接口连接到该 IP 地址。还有 Google:对你没有更好地记录你的一些 API 表示不满!
I think this is an extremely poorly documented method, and while an above comment saying "consider it a ping" might be a reasonable interpretation, I don't think it's correct. The fact that it takes an int as a host address suggests it is a much lower-level method than that, and the comment in the JavaDoc
This method requires the caller to hold the permission CHANGE_NETWORK_STATE
is another clue, suggesting that this makes a change in the internal routing table of the device. This link provides a better explanation:This explanation makes MUCH more sense, considering the permission required. It also appears that it will not work with WiFi. So, it appears what this method is useful for is the following: You wish to ensure that the connection made to a particular host will be made via a SPECIFIC interface and that interface is not WiFi. This might make sense for a long-term, low traffic, battery efficient connection, such as when you wish to keep a socket open to a server and wait for the server to send the occasional message. The mobile data interface would make more sense than WiFi, since you wouldn't need to keep the WiFi radio active the whole time, and the mobile network radio is always on anyway. Incidentally, this is EXACTLY how an iPhone's server "push" mechanism works: It keeps a socket to an Apple server constantly connected over the mobile data interface, waiting for the server to say something.
So, in opposition to the (currently chosen) correct answer, I suggest that the answer to the asker's question:
Does this routing means changing the WIFI to 3G or vice versa??
is actually, "Yes, sort of!" If the method returns true, the caller is assured that connections to that IP address will happen over the indicated interface.And to Google: Boo on you for not documenting some of your APIs better!
方法
requestRouteToHost()
不会将 wifi 更改为 3G,反之亦然!官方文档 :
参数
networkType
将流量路由到指定主机的网络类型hostAddress
需要路由的主机的 IP 地址返回
成功时返回 true
,失败时返回 false
Method
requestRouteToHost()
does not change wifi to 3G or vice versa!Official Documentation :
Parameters
networkType
the type of the network over which traffic to the specified host is to be routedhostAddress
the IP address of the host to which the route is desiredReturns
true on success
,false on failure