简体   繁体   English

Android Lollipop网络API和OkHttp

[英]Android Lollipop Network API and OkHttp

I running into an issue routing network calls using OkHttp. 我遇到了使用OkHttp路由网络呼叫的问题。 I'm trying to send HTTP requests to a local device (via the device's ip) using an Access Point that has no internet access. 我正在尝试使用无法访问互联网的访问点将HTTP请求发送到本地设备(通过设备的ip)。 Due to some changes in Android 5.0, OkHttp will try to route the request over data instead, which then fails. 由于Android 5.0中的某些更改,OkHttp将尝试改为通过数据路由请求,然后失败。

In Android 5.0's Network API, I could use the NetworkCapabilities class along with the NetworkRequest.Builder in order to tell the request to only go over Wi-Fi, However, I'm already using OkHttp and I don't want to overhaul my code. 在Android 5.0的Network API中,我可以将NetworkCapabilities类与NetworkRequest.Builder一起使用,以告诉请求仅通过Wi-Fi,但是,我已经在使用OkHttp,并且我不想进行大修我的代码。 Is there anyway to do a similar thing with OkHttp? 无论如何,是否可以使用OkHttp做类似的事情?

I came up with a workaround: 我想出了一种解决方法:

final Network currentNetwork = (Network) network;
newClientBuilder.socketFactory(((Network)network).getSocketFactory())
    .dns(new Dns() {
          @Override
          public List<InetAddress> lookup(String hostname) throws UnknownHostException {
                        return Arrays.asList(currentNetwork.getAllByName(hostname));
                    }
                });

We'd like to eventually include such functionality directly in OkHttp, and you might be the first volunteer/victim to test things out. 我们最终希望将这些功能直接包含在OkHttp中,您可能是第一个测试事物的志愿者/受害者。 What you probably want to do is use OkHttp 2.6's new Dns interface to call through to the network of your choice. 您可能想做的是使用OkHttp 2.6的新Dns接口调用您选择的网络。 If that works, either the IP address will cause OkHttp to do the right thing anyway, or you'll also need to replace the SocketFactory . 如果可行,则IP地址仍然会使OkHttp做正确的事情,或者您需要替换SocketFactory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM