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"); } } 我收到一个错误
解决方法
看例子和我的简单
library
... try{ WebsocketClient.dispatcher().cancelAll();// to cancel all requests }... 触发失败(…)您可以重新连接 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |