如何重新连接okhttp-ws

And*_*rey 5 java android okhttp

如果IP地址更改或连接失败,如何正确重新连接?

我尝试重新连接 okhttp-ws

.....

    @Override
    public void onFailure(IOException e, okhttp3.Response response) {
      try {
               connecting();
            } catch (Exception e1) {
            Timber.e(e1, "onFailure");
        }
    }

    @Override
    public void onClose(int code, String reason) {
        Timber.d("Connection unexpectedly closed");
        connecting();
    }

    public void connecting() {
    if (wsClient == null) {
        wsClient = builder.build();
    if (call != null) call.cancel();
    call = WebSocketCall.create(wsClient, request);
    try {
        lock.lockInterruptibly();
        try {  call.enqueue(listener);
        } finally {
            lock.unlock();
        }
    } catch (InterruptedException e) {
        Timber.e(e, "connecting error");
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误

java.lang.RuntimeException: Unable to start service ... (has extras) }: java.util.concurrent.RejectedExecutionException: Task okhttp3.RealCall$AsyncCall@3f946389 从 java.util.concurrent.ThreadPoolExecutor@d784f8e[Terminated, pool 被拒绝大小 = 0,活动线程 = 0,排队任务 = 0,已完成任务 = 1] 在 android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3320)

小智 5

看例子和我的简易图书馆

...
try{
WebsocketClient.dispatcher().cancelAll();// to cancel all requests
}...
Run Code Online (Sandbox Code Playgroud)

触发失败 ( ... ) 您可以重新连接