改造 2 RequestBody writeTo() 方法调用两次

2022-09-02 02:29:03

Retrofit 2 RequestBody writeTo() 方法调用两次,我使用的代码如下:

ProgressRequestBody requestVideoFile = new ProgressRequestBody(videoFile, new ProgressRequestBody.UploadCallbacks() {

    VideoUploadStore store = new VideoUploadStore();

    @Override
    public void onProgressUpdate(int percentage) {
        if (!mIsCancelled) {
            Log.i("UploadServiceManager", "Read Percentage : " + percentage);
            data.setUploadPercentage(percentage);
            store.updateUploadData(data);
        }
    }

    @Override
    public void onError() {
        if(!mIsCancelled) {
            data.setUploadPercentage(0);
            store.updateUploadData(data);
        }
    }

    @Override
    public void onFinish() {

    }
});
MultipartBody.Part multipartVideo = MultipartBody.Part.createFormData("File", videoFile.getName(), requestVideoFile);

答案 1

下面的解决方案可能会帮助您 ,尽管可能为时已晚。:p

删除 Api 客户端中不会执行 writeTo() 函数两次的对象。基本上,首先通过调用 writeTo() 加载数据缓冲区(用于内部日志记录目的),然后再次调用 writeTo() 将数据上传到服务器。HttpLoggingInterceptorHttpLoggingInterceptor

  HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
  logging.setLevel(HttpLoggingInterceptor.Level.BODY);
  httpClient.addInterceptor(logging);

答案 2

将日志级别从 BODY 降低到 HEADERS,BASIC 或 NONE 为我解决了这个问题


推荐