我使用改装上传图片。当图片很小时效果很好,但是当我选择一张大图片时,请求的执行次数比预期的要多。
例如,如果我选择了9张大图片,它应该执行上传请求9次,但它会执行多次,有时会上传14张图片。我发现有些图片上传了两次,我不知道是不是因为okhttp的重新连接机制
private void saveToServer() {
List<Call<MediaResponse>> calls = generateCallList();
Log.e("", "calls-->" + calls.size());
for (int i = 0; i < calls.size(); i++) {
Call<MediaResponse> call = calls.get(i);
Log.e("", "call**");
call.enqueue(new Callback<MediaResponse>() {
@Override
public void onFailure(Call<MediaResponse> call, Throwable t) {
}
@Override
public void onResponse(Call<MediaResponse> call,
Response<MediaResponse> response) {
if (response.isSuccessful()) {
// TODO 合并刷新列表
Log.e("", "response-->" + response.body());
}
}
});
}
}
private List<Call<MediaResponse>> generateCallList() {
GuoRetrofit retrofit = new GuoRetrofit();
AddSingleMediaService service = retrofit.getAddSingleMediaService();
List<Call<MediaResponse>> calls = new ArrayList<Call<MediaResponse>>();
for (String path : ImageListAdapter.mSelectedImage) {
Call<MediaResponse> call = service.addSingleMedia(getMeta(path),
getfile(path));
calls.add(call);
}
return calls;
}`
当我选择5张图片时,日志是: enter image description here
* 05-05 15:56:32.238:E /(16416):path - &gt; /system/media/Pre-loaded/Pictures/Picture_01.jpg 05-05 15:56:32.248:E /(16416):path - &gt; /system/media/Pre-loaded/Pictures/Picture_02.jpg 05-05 15:56:32.248:E /(16416):path - &gt; /system/media/Pre-loaded/Pictures/Picture_03.jpg 05-05 15:56:32.258:E /(16416):path - &gt; /system/media/Pre-loaded/Pictures/Picture_04.jpg 05-05 15:56:32.258:E /(16416):path - &gt; /system/media/Pre-loaded/Pictures/Picture_05.jpg 05-05 15:56:32.258:E /(16416):电话 - > 5;
05-05 15:56:32.258:E /(16416):call**retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@c6c242a
05-05 15:56:32.258:E /(16416):call**retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@308c2fb8
05-05 15:56:32.258:E /(16416):call**retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@348058f6
05-05 15:56:32.258:E /(16416):call**retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@9b08f64
05-05 15:56:32.268:E /(16416):call**retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@2dfaf993 *