本文介紹了如何重新連接okhttp-ws的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如果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.Rty異常:無法啟動服務…(有
Extras)}:java.util.concurrent.RejectedExecutionException:任務
OkHtp3.RealCall$AsyncCall@3f946389被拒絕,來自
Java.util.concurrent.ThreadPoolExecutor@d784f8e[Terminated,池大小
=0,活動線程=0,排隊任務=0,已完成任務=1]
在…
Android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3320)
推薦答案
參見Example and My Easylibrary
...
try{
WebsocketClient.dispatcher().cancelAll();// to cancel all requests
}...
觸發失敗(…)您可以重新連接
這篇關于如何重新連接okhttp-ws的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,