Я использую Retrofit для загрузки массива файлов на сервер. Когда я добавляю эту строку, Retrofit выдает ошибку «неожиданный конец потока». Когда я его убрал, ошибка решилась.
mutableLiveData.setValue(totalPercent);
Класс загрузки:
public class UploadRepository{
private MutableLiveData<Integer> mutableLiveData;
private Call<UploadResponse> callUploadReport;
private long totalLength = 0;
private long uploadedLength = 0;
public UploadRepository() {
}
public MutableLiveData<Integer> uploadReport(ReportMetadata metadata, List<Document> documentList){
mutableLiveData = new MutableLiveData<>();
APIService apiService = RetroClass.getAPIService();
MultipartBody.Part[] partArray = new MultipartBody.Part[documentList.size()];
for (int i = 0; i < documentList.size(); i++) {
//...
//...
ProgressRequestBody requestBody = new ProgressRequestBody(file, type, new ProgressRequestBody.UploadCallbacks(){
@Override
public void onFileUploaded(long length, long total) {
int totalPercent = (int)(100 * (uploadedLength+length) / totalLength);
//========================================================================================
//>>>>>>>>>>>>>>I get error when add this line, That is soloved when removed that!!!
//========================================================================================
mutableLiveData.setValue(totalPercent);
}
@Override
public void onFinished(long fileLength) {
uploadedLength += fileLength;
}
@Override
public void onError() {
}
});
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
partArray[i] = part;
}
//...
//...
callUploadReport = apiService.uploadReport(partArray, userNameReq,titleReq,timeReq,categoryReq);
callUploadReport.enqueue(new Callback<UploadResponse>() {
@Override
public void onResponse(Call<UploadResponse> call, Response<UploadResponse> response) {
//...
//...
}
@Override
public void onFailure(Call<UploadResponse> call, Throwable t) {
Log.i("upload","failure>>>"+t.getMessage());
//========================================================================================
// In this place I get "unexpected end of stream" error
//========================================================================================
if (!call.isCanceled())
mutableLiveData.setValue(-1);
}
});
return mutableLiveData;
}
}
Кто-нибудь может мне помочь?