I have an App that can record and upload video file.
I have a mechanism that logs onFailure Throwable
on every file upload.
I have noticed that from a thousand devices using the App, there are some that have constant timeouts and have poor success upload ratio.
The problematic device is: Asus ZenPad C 7.0 Z170C
I have bought the device and started testing it, but first lets give you a flavour of what code I use.
In Activity:
FileUploadService service = ServiceGenerator.createService(FileUploadService.class);
File file = new File(getRealPathFromURI(mediaForUpload.mediaUri));
ProgressRequestBody fileBody = new ProgressRequestBody(file,
new ProgressRequestBody.UploadCallbacks() {
@Override
public void onProgressUpdate(int uploadPercentage) {
// set current progress
}
@Override
public void onError() {}
@Override
public void onFinish() {}
});
MultipartBody.Part body = MultipartBody.Part.createFormData(mediaForUpload.mediaNameWithExtension, mediaForUpload.mediaNameWithExtension, fileBody);
Call<ResponseBody> call = service.upload(body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
if (response.isSuccessful()) {
} else {
//Log
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
//Log
}
});
In ServiceGenerator:
public static <S> S createService(Class<S> serviceClass) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.build();
Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl("https://some.nice.api/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(client).build();
return retrofit.create(serviceClass);
}
In FileUploadService:
@Multipart
@POST("/somepath")
Call<ResponseBody> upload(@Part MultipartBody.Part file);
So as you can see my timeout is 120 seconds, interesting but this is not enough to successfully upload a 15MB video file without timeout.
I have made couple of test from the Asus Zenpad C 7.0 and Samsung Tab 3 7" just to show you the difference. (The test is on 2.4GHz network, 5 meters from the router; Tab 3 average upload speed: 1MB/s, Zenpad C7 average upload speed: ~30KB/s if miracle occurs)
Screenshots from Android Monitor:
- Samsung Tab 3 7"
- Asus Zenpad C 7.0
failed with 120 timeout after this:
success after these two, but waiting 1:35 minutes:
I have tested OkHttp3 Multipart upload, just to be sure that this is not Retrofit 2 issue and the results are the same.