请求路由到主机 android
大家好! 我已经厌倦了 android 的 ConnectivityManager 类。我已经尝试了 5 个小时才让 requestRouteToHost 正常工作。我在模拟器上运行我的代码,但 requestRouteToHost 总是失败。我知道我有连接,因为我调用了 getActiveNetworkInfo() 并且它已连接。我添加了 ACCESS_NETWORK_STATE 和 CHANGE_NETWORK_STATE 权限但无济于事。任何提示将不胜感激。
编辑 - - - - InetAddress.isReachable 方法也失败。我使用的是文档中指定的主机名 java.sun.com 。我认为这是模拟器的问题。 山姆
Greetings everyone!
I'm getting rather fed up with android's ConnectivityManager class. I've been trying for 5 hours to get the requestRouteToHost to work. I'm running my code on the emulator but the requestRouteToHost always fails. I know I have connectivity because I called getActiveNetworkInfo() and it was connected. I've added the ACCESS_NETWORK_STATE and CHANGE_NETWORK_STATE permissions to no avail. Any tips would be greatly appreciated.
EDIT--------
The method InetAddress.isReachable fails as well. I was using the hostname java.sun.com as specified in the docs. I think this is a problem with the emulator.
Sam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来 requestRouteToHost() 没有针对 WiFi 实现。 WifiStateTracker 私有类没有 requestRouteToHost() 实现。相反,基类 NetworkStateTracker 的默认实现始终返回 false。
Looks like requestRouteToHost() is not implemented for WiFi. WifiStateTracker private class does not have requestRouteToHost() implementation. Instead, default implementation from base class NetworkStateTracker always returns false.
我建议使用真实主机测试
requestRouteToHost()
。我不仅不相信localhost
作为一个名称一定会在 Android 上被识别,而且它将是设备/模拟器本身,它既不会通过 WiFi 也不会通过 3G 出去,所以我希望requestRouteToHost()
失败。您还可能会遇到从何处获取
lookupHost()
的问题。例如,我不确定此实现是否支持localhost
。I would recommend testing
requestRouteToHost()
with a real host. Not only am I unconvinced thatlocalhost
as a name is necessarily recognized on Android, it's going to be the device/emulator itself, which would go out via neither WiFi nor 3G, so I would expectrequestRouteToHost()
to fail.You might also have a problem with wherever you are getting
lookupHost()
from. For example, I am not sure if this implementation supportslocalhost
.您所做的 requestRouteToHost(lookupHost()) 组合非常危险,
请参阅您的代码:
在这里,您首先进行主机查找,然后请求指向该主机的路由。它仅适用于静态地址(如本地主机),无需进行真正的查找。如果您需要 DNS 查找来解析主机地址,则很容易失败。
一般来说, requestRouteToHost 仅适用于静态地址。
The requestRouteToHost(lookupHost()) combination you are doing is very risky
See your code:
Here, you first made a host lookup, and next you are requesting a route to it. It will work fine only for static addresses (like localhost), where no real lookup is done. If you need a DNS lookup to resolve host address, it will fail easily.
Generally, requestRouteToHost is handy for static addresses only.