localhorst
localhorst

Reputation: 365

Strange connection blocking / hanging with OkHttpClient

I have an app consisting of a BroadcastReceiver that is called on network connectivity changes (basically I just want to call certain URLs when I connect to a certain SSID).

I have an odd issue with hanging OkHttp requests. Sometimes the request gets processed right away, sometimes it takes 10 or 20 seconds. I have already tried to set timeouts for connection, reading and writing for the used OkHttpClient to no avail. The request is issued after the phone has connected to the network. I have also tried to fire the request in a seperate thread with a delay of 2 seconds, but that didn't change anything.

EDIT: I've added a network interceptor. Interestingly, even the first request is delayed significantly most of the times. However, if I start off with an IP address rather than google.com, the first request instantly pops up in the interceptor. Is there a possible DNS issue?

OkHttpClient setup

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();

BroadcastReceiver

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*/);
}

Receiver in Manifest

        <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 result, note the time difference

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}

Upvotes: 4

Views: 1987

Answers (1)

localhorst
localhorst

Reputation: 365

I have just verified (by adding a custom dns resolver) that the requests are blocking due to slow DNS resolving.

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}

Upvotes: 4

Related Questions