Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set timeout for OkHttpClient? [duplicate]

I am working on a native android project (Java) testing on a physical device with android 4.4.2. My OkHttpClient websocket connects but times out after 10 seconds, this is what I am trying to use to change the timeout setting.

OkHttpClient client = new OkHttpClient();
client.setReadTimeout(0, TimeUnit.MILLISECONDS);

but it is saying Cannot resolve method setReadTimeout(int, java.util.concurrent.TimeUnit)

These are my imports:

import android.util.Log;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.ws.WebSocket;
import okhttp3.ws.WebSocketCall;
import okhttp3.ws.WebSocketListener;
import okio.Buffer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

and in my gradle file I have compile 'com.squareup.okhttp3:okhttp-ws:3.4.1'

like image 313
CookieMonster Avatar asked Sep 02 '16 15:09

CookieMonster


People also ask

How do I set timeout in OkHttp?

Connect Timeout A connect timeout defines a time period in which our client should establish a connection with a target host. By default, for the OkHttpClient, this timeout is set to 10 seconds. However, we can easily change its value using the OkHttpClient. Builder#connectTimeout method.

What is the default timeout for retrofit Android?

By default, Retrofit 2 uses the following timeouts: Call timeout – 0 (no timeout) Connection timeout – 10 seconds. Read timeout – 10 seconds.


1 Answers

did you try with builder?

OkHttpClient client = new OkHttpClient.Builder()
    .connectTimeout(10, TimeUnit.SECONDS)
    .writeTimeout(10, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build();

UPDATE

try compiling compile 'com.squareup.okhttp3:okhttp:3.4.1'

like image 117
AmirG Avatar answered Oct 04 '22 18:10

AmirG