android - OkHttpClient 奇怪的连接阻塞/挂起

标签 android okhttp

我有一个由 BroadcastReceiver 组成的应用程序,它会在网络连接发生变化时调用(基本上我只想在连接到某个 SSID 时调用某些 URL)。

我有一个关于挂起 OkHttp 请求的奇怪问题。有时请求会立即得到处理,有时需要 10 或 20 秒。我已经尝试为使用过的 OkHttpClient 设置连接、读取和写入超时,但无济于事。该请求在手机连接到网络后发出。我还尝试在延迟 2 秒的单独线程中触发请求,但这并没有改变任何东西。

编辑:我添加了一个网络拦截器。有趣的是,大多数时候即使是第一个请求也会被显着延迟。但是,如果我从 IP 地址而不是 google.com 开始,第一个请求会立即在拦截器中弹出。可能存在 DNS 问题吗?

OkHttpClient 设置

client = new OkHttpClient.Builder()
.writeTimeout(3, TimeUnit.SECONDS)
.readTimeout(3, TimeUnit.SECONDS)
.connectTimeout(3, TimeUnit.SECONDS)
.followRedirects(true)
.addNetworkInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    Log.d(TAG, "Request to URL: " + request.url());

                    Response response = chain.proceed(request);

                    return response;
                }
            })
.build();

广播接收器

public void onReceive(Context context, Intent intent) {
   /* ... */
   client.newCall(new Request.Builder()
                        .url(url)
                        .build()).enqueue(/* logging callback with
                        Log.d(TAG, response.toString()); on success*/);
}

list 中的接收器

        <receiver
        android:name=".WifiReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
    </receiver>

Logcat结果,注意时差

11-24 22:19:49.717 17511 17511 I WifiReceiver:  -- Wifi connected ---
11-24 22:19:49.796 D WifiReceiver: Request to URL: http://172.217.22.78/
11-24 22:20:00.300 D WifiReceiver: Request to URL: http://www.google.com/
11-24 22:20:11.363 D WifiReceiver: Request to URL: http://www.google.de/?gfe_rd=cr&ei=gFk3WL3CDdHnugSQ262QCA
11-24 22:20:11.750 17511 18242 D WifiReceiver: Response{protocol=http/1.1, code=200, message=OK, url=http://www.google.de/SQ262QCA}

最佳答案

我刚刚验证(通过添加自定义 DNS 解析器)请求因 DNS 解析缓慢而被阻止。

11-24 22:29:39.317 D/WifiReceiver: Lookup 172.217.22.78
11-24 22:29:39.372 D/WifiReceiver: Request to URL: http://172.217.22.78/
11-24 22:29:39.425 D/WifiReceiver: Lookup www.google.com
11-24 22:29:49.953 D/WifiReceiver: Request to URL: http://www.google.com/
11-24 22:29:50.291 D/WifiReceiver: Lookup www.google.de
11-24 22:30:00.775 D/WifiReceiver: Request to URL: http://www.google.de/?gfe_rd=cr&ei=zVs3WLmoNdPLugTk8IKoCA
11-24 22:30:01.197 D/WifiReceiver: Response{protocol=http/1.1, code=200, message=OK, url=http://www.google.de/?gfe_rd=cr&ei=zVs3WLmoNdPLugTk8IKoCA}

关于android - OkHttpClient 奇怪的连接阻塞/挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40793786/

相关文章:

java - 如何在 android 中加载 5 行 5 列顶 View 的 Sprite 表?

WebView Activity 中的 Android 左/右滑动手势(点击链接和垂直滚动)

java - TYPE_SYSTEM_OVERLAY 内的 GestureDetector

java - 在表单数据部分指定 Content-Type 为 application/json

android - 禁用okhttp的缓存

android - 在 Android < 7.1.1 上制作应用程序信任 Let's Encrypt ISRG Root X1

java - 使用 FragmentStatePagerAdapter 在 ViewPager 中刷新 fragment

android - 在项目 'prepareReleaseUnitTestDependencies' 中找不到任务 ':app'

android - OkHttp 请求即使在网络出现故障时也能保持运行

java - 使用 OKHTTP3 上传带有进度条的多个文件