你能解释一下 android 中 requestRouteToHost() 的功能吗?

发布于 2024-11-27 15:46:14 字数 1167 浏览 2 评论 0原文

在我的代码中,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2024-12-04 15:46:14

我认为这是一种记录极差的方法,虽然上面的评论说“将其视为 ping”可能是一个合理的解释,但我认为这是不正确的。事实上,它采用 int 作为主机地址,这表明它是一个比这低得多的方法,JavaDoc 中的注释 此方法要求调用者持有 CHANGE_NETWORK_STATE 权限 是另一个线索,表明这会更改设备的内部路由表。 此链接提供了更好的解释:

requestRouteToHost() 不会在任何网络上建立连接,它
仅确保指定主机的任何流量都将被路由
通过指定的网络类型(wifi 或移动)。连接性必须
指定网络上已存在。

考虑到所需的许可,这种解释更有意义。它似乎也无法与 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:

requestRouteToHost() doesn't establish connectivity on any network, it
only ensures that any traffic for the specified host will be routed
via the specified network type (wifi or mobile). Connectivity must
already exist on the specified network.

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!

猫卆 2024-12-04 15:46:14

方法 requestRouteToHost() 不会将 wifi 更改为 3G,反之亦然!

官方文档

public boolean requestRouteToHost (int networkType, int hostAddress) 

确保存在网络路由以通过指定的网络接口将流量传送到指定的主机。尝试添加已存在的路由将被忽略,但被视为成功。

  • 参数

    networkType 将流量路由到指定主机的网络类型

    hostAddress 需要路由的主机的 IP 地址

  • 返回

    成功时返回 true失败时返回 false

Method requestRouteToHost() does not change wifi to 3G or vice versa!

Official Documentation :

public boolean requestRouteToHost (int networkType, int hostAddress) 

Ensure that a network route exists to deliver traffic to the specified host via the specified network interface. An attempt to add a route that already exists is ignored, but treated as successful.

  • Parameters

    networkType the type of the network over which traffic to the specified host is to be routed

    hostAddress the IP address of the host to which the route is desired

  • Returns

    true on success, false on failure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文