请求路由到主机 android

发布于 2024-08-26 05:48:06 字数 339 浏览 2 评论 0原文

大家好! 我已经厌倦了 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 技术交流群。

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

发布评论

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

评论(3

葬花如无物 2024-09-02 05:48:06

看起来 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.

始于初秋 2024-09-02 05:48:06

我建议使用真实主机测试 requestRouteToHost()。我不仅不相信 localhost 作为一个名称一定会在 Android 上被识别,而且它将是设备/模拟器本身,它既不会通过 WiFi 也不会通过 3G 出去,所以我希望 requestRouteToHost() 失败。

您还可能会遇到从何处获取 lookupHost() 的问题。例如,我不确定此实现是否支持localhost

I would recommend testing requestRouteToHost() with a real host. Not only am I unconvinced that localhost 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 expect requestRouteToHost() to fail.

You might also have a problem with wherever you are getting lookupHost() from. For example, I am not sure if this implementation supports localhost.

拥抱我好吗 2024-09-02 05:48:06

您所做的 requestRouteToHost(lookupHost()) 组合非常危险,

请参阅您的代码:

conn_man.requestRouteToHost(ConnectivityManager.TYPE_WIFI, lookupHost("localhost")))

在这里,您首先进行主机查找,然后请求指向该主机的路由。它仅适用于静态地址(如本地主机),无需进行真正的查找。如果您需要 DNS 查找来解析主机地址,则很容易失败。
一般来说, requestRouteToHost 仅适用于静态地址。

The requestRouteToHost(lookupHost()) combination you are doing is very risky

See your code:

conn_man.requestRouteToHost(ConnectivityManager.TYPE_WIFI, lookupHost("localhost")))

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.

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