加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何重新连接okhttp-ws

发布时间:2020-12-15 04:33:48 所属栏目:Java 来源:网络整理
导读:如果IP地址改变或连接失败,如何正确重新连接? 我试着重新连接okhttp-ws ….. @Override public void onFailure(IOException e,okhttp3.Response response) { try { connecting(); } catch (Exception e1) { Timber.e(e1,"onFailure"); } } @Override public
如果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");
    }
}

我收到一个错误

java.lang.RuntimeException: Unable to start service … (has
extras) }: java.util.concurrent.RejectedExecutionException: Task
okhttp3.RealCall$AsyncCall@3f946389 rejected from
java.util.concurrent.ThreadPoolExecutor@d784f8e[Terminated,pool size
= 0,active threads = 0,queued tasks = 0,completed tasks = 1]
at
android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3320)

解决方法

看例子和我的简单 library

...
try{
WebsocketClient.dispatcher().cancelAll();// to cancel all requests
}...

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读