你能解释一下android中 requestRouteToHost()的功能吗?[英] Can you explain the Functionality of requestRouteToHost() in android?

本文是小编为大家收集整理的关于你能解释一下android中 requestRouteToHost()的功能吗?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我的代码中我使用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;
 }

感谢

推荐答案

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

官方文档 <强>:

public boolean requestRouteToHost (int networkType, int hostAddress) 

确保存在网络路由以通过指定的网络接口向指定主机提供流量.尝试添加已经存在的路由被忽略,但被视为成功.

  • 参数

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

    hostAddress所需路线的主机的IP地址

  • 返回

    true on success,false on failure

其他推荐答案

我认为这是一个非常糟糕的方法,而上面的评论说"认为它是一个ping"可能是一个合理的解释,而我认为这不认为这是正确的.它需要一个int作为主机地址的事实表明它是一个远低级的方法,而javadoc This method requires the caller to hold the permission CHANGE_NETWORK_STATE中的评论是另一个线索,这表明这在设备的内部路由表中会发生变化. 此链接提供更好的解释:

requestrouteTohost()不在任何网络上建立连接 只能确保指定主机的任何流量都将被路由 通过指定的网络类型(WiFi或Mobile).连接必须 已经存在于指定的网络上.

考虑所需的许可,这种解释更有意义.它还似乎它不会与WiFi合作.因此,它出现此方法的用途是以下内容:您希望确保将通过特定接口进行对特定主机的连接,并且该接口不是WiFi.这可能对长期,低流量,电池有效连接有意义,例如当您希望将套接字打开到服务器并等待服务器发送偶尔的消息时.移动数据界面会比WiFi更有意义,因为您无需将WiFi无线电保持一定时间,并且无论如何,移动网络无线电总是在开启.顺便提一下,这正是iPhone的服务器"推送"机制的工作原理:它将套接字保留到Apple服务器上,苹果服务器不断通过移动数据界面连接,等待服务器来说些什么.

所以,在反对(目前被选中)正确答案中,我建议问答的问题的问题:Does this routing means changing the WIFI to 3G or vice versa??实际上是,"是的,有点!"如果该方法返回true,则查询呼叫者将在指示的界面上发生与该IP地址的连接.

和google:嘘您没有记录您的一些API更好!

本文地址:https://www.itbaoku.cn/post/102395.html

问题描述

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

推荐答案

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

其他推荐答案

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!