使用Retrofit2 / okhttp3上传文件,上传操作总是执行两次,一次快速,其他慢

时间:2016-07-12 03:19:32

标签: java okhttp retrofit2

我使用ProgressRequestBody显示上传操作的进度。

@Override
public void writeTo(BufferedSink sink) throws IOException {
    // see https://github.com/square/okhttp/issues/1587
    // JakeWharton commented on 28 Apr 2015  's answer
    final BufferedSink progressSink = Okio.buffer(new ForwardingSink(sink) {
        long bytesWritten = 0L;
        long contentLength = 0L;

        @Override
        public void write(Buffer source, long byteCount) throws IOException {
            if (contentLength == 0) {
                contentLength = contentLength();
            }
            bytesWritten += byteCount;
            mListener.onProgressUpdate(bytesWritten,contentLength,bytesWritten == contentLength);
            System.out.println("--byte--"+bytesWritten);
            super.write(source, byteCount);
        }
    });
    requestBody.writeTo(progressSink);
    System.out.println("--byte-- start---");
    progressSink.flush();
    System.out.println("--byte-- end---");
}

每次执行上传操作时都会调用此方法两次。 起初,我认为问题可能是添加到okhttpclient中的日志拦截器,但事实并非如此。谁能帮帮我?谢谢

更多代码:

public interface UploadInterface {

@Multipart
@POST("path")
Call<JsonBase<Result>> uploadFile(
        @Query("_appTicket") String cookie, 
        @Query("Id") String id, 
        @Part MultipartBody.Part requestBody
);
}

上传动作:

final ProgressRequestBody progressRequestBody
                            = new ProgressRequestBody(
                            RequestBody.create(
                                    MediaType.parse("multipart/form-data"),
                                    tempZip
                            )
                    );
MultipartBody.Part part = MultipartBody.Part
                            .createFormData("file","upload",progressRequestBody);
                    final Call<JsonBase<JsonUploadResult>> call 
                            = uplocaInterface.uploadFile(cookie,s,part);

3 个答案:

答案 0 :(得分:4)

似乎你正在使用HttpLoggingInterceptor,它为Request体调用了writeTo。您可以忽略该问题,因为应该为发布版本禁用HttpLoggingInterceptor或覆盖HttpLoggingInterceptor的拦截方法,并为此特定上载请求禁用它。

答案 1 :(得分:1)

我使用此代码来跟踪http请求进度。

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        Source source = null;
        try {
            source = Okio.source(file);
            long total = 0;
            long read;
            Handler handler = new Handler(Looper.getMainLooper());

            while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
                total += read;
                sink.flush();

                final int percent = (int) (total / (float)file.length() * 100);

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        listener.transferred(percent);
                    }
                });
            }
        } finally {
            Util.closeQuietly(source);
        }
    }

删除所有http客户端拦截器,以避免两次调用方法。

答案 2 :(得分:0)

您好,请查看以下博客文章

https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server

对你的问题有很好的解释。