6

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'

CookieMonster
  • 492
  • 6
  • 18

1 Answers1

16

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'

AmirG
  • 615
  • 8
  • 18
  • I just tried them, same thing, `Cannot resolve method ... ` – CookieMonster Sep 02 '16 at 15:27
  • @CookieMonster try the updated answer – AmirG Sep 02 '16 at 15:29
  • I added that to my gradle while keeping my old one and it doesn't seem to change anything. I tried removing my original one and keeping yours but I need my original for later parts where I use `WebSocketCall ws = WebSocketCall.create(...` – CookieMonster Sep 02 '16 at 15:36
  • Are you sure you have your gradle setting working online ? i just tried with the okhttp ws library that you are using and i can access the methods for setting the timeouts with builder – AmirG Sep 02 '16 at 15:53
  • @CookieMonster check my second update – AmirG Sep 02 '16 at 15:55
  • @CookieMonster please notice that is not setReadTimeOut() is readTimeOut() – AmirG Sep 02 '16 at 15:57
  • Oh I see, yeah that works, I was trying to add it to the wrong builder. I was trying to do: `WebSocketCall ws = WebSocketCall.create(client, new Request.Builder().connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .url(finalURL) .addHeader("Cookie", tok) .build());` – CookieMonster Sep 02 '16 at 16:18
  • Workable answer guys. Thanks – Naveed Ahmad Jul 09 '17 at 19:37