或者认为适用于这种情况的拦截器?
我们的应用程序使用 OkHttp 下载文件(新版本的应用程序、每日数据库等)
有时服务器会在应用程序流式传输字节时失败(顺便说一句,recvfrom 失败:ECONNRESET)
所以为了解决这个问题,我只想编写 OkHttp 重试拦截器。但这似乎适用于非流式传输的操作。
是否有解决方案(如拦截器)来处理这种情况?
为了更清楚地展示
0%===============================100%(刚开始直播)
0% === ============================100%(完成10%)
0% ====== =========================100%(完成20%)
0% ====== ==========================100%(ECONNRESET - 对等方重置连接)
就在此时,流媒体停止了。我希望 OkHttp 的事情是识别这种情况,然后从头开始(不是从 20%)
相关代码在这里,注意注释
Call call = client.newCall(new Request.Builder().url(url).get().build());
Response response = call.execute();
// PROBLEM DOES NOT OCCUR THERE
// PROBLEM DOES NOT OCCUR THERE
// PROBLEM DOES NOT OCCUR THERE
if (response.code() == 200 || response.code() == 201) {
InputStream inputStream = null;
try {
long downloaded = 0;
byte[] buff = new byte[1024 * 4];
inputStream = response.body().byteStream();
long target = response.body().contentLength();
while (true) {
// EXCEPTION OCCURS THERE
// EXCEPTION OCCURS THERE
// EXCEPTION OCCURS THERE
int read = inputStream.read(buff);
if (read == -1) {
break;
}
downloaded += read;
}
...
} catch (IOException e) {
// EXCEPTION SAYS
// ECONNRESET - Connection reset by peer
...
}
}