与LiveData配合使用时,出现“意外的流结束”错误

时间:2020-04-13 07:02:25

标签: android retrofit retrofit2 okhttp android-livedata

我使用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;
      }

    }

有人可以帮助我吗?

0 个答案:

没有答案