Android upload 文件回调进度

1.继承RequestBody,实现可以监听进度的FileRequestBody

public class FileRequestBody extends RequestBody {
//请求
private RequestBody mRequestBody;
private UploadListener mUploadListener;
private CountingSink mCountingSink;

public FileRequestBody(RequestBody mRequestBody, UploadListener mUploadListener) {
    this.mRequestBody = mRequestBody;
    this.mUploadListener = mUploadListener;
}

@Nullable
@Override
public MediaType contentType() {
    return mRequestBody.contentType();
}

@Override
public long contentLength() throws IOException {
    try {
        return mRequestBody.contentLength();
    } catch (IOException e) {
        e.printStackTrace();
        return -1;
    }
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
    BufferedSink bufferedSink;
    mCountingSink = new CountingSink(sink);
    bufferedSink = Okio.buffer(mCountingSink);

    mRequestBody.writeTo(bufferedSink);
    //必须调用flush,否则最后一部分数据可能不会被写入
    bufferedSink.flush();

}


class CountingSink extends ForwardingSink {

    private long bytesWritten = 0;

    public CountingSink(Sink delegate) {
        super(delegate);
    }

    @Override
    public void write(Buffer source, long byteCount) throws IOException {
        super.write(source, byteCount);
        bytesWritten += byteCount;
        mUploadListener.onRequestProgress(bytesWritten, contentLength());
    }
}

}
进度监听回调

public interface UploadListener {
void onRequestProgress(long bytesWritten, long contentLength);
}

2.拦截器定义
public class UpLoadProgressInterceptor implements Interceptor {
private UploadListener mUploadListener;

public UpLoadProgressInterceptor(UploadListener mUploadListener) {
    this.mUploadListener = mUploadListener;
}


@Override
public Response intercept(Chain chain) throws IOException {
    Request request =chain.request();
    if(null==request.body()){
        return chain.proceed(request);
    }
    Request builder =request.newBuilder()
            .method(request.method(),new FileRequestBody(request.body(),mUploadListener))
            .build();
    return chain.proceed(builder);
}

}

在获取Retrofit 时候,用自定义的拦截器:
private static TjRetrofitService retroUploadFileService;
public static TjRetrofitService getUploadWithProgressService(UploadListener uploadListener){
UpLoadProgressInterceptor interceptor = new UpLoadProgressInterceptor(uploadListener);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.retryOnConnectionFailure(true)
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
.readTimeout(TIMEOUT,TimeUnit.SECONDS)
.writeTimeout(TIMEOUT,TimeUnit.SECONDS)
.build();
if(retroUploadFileService ==null){
retroUploadFileService =new Retrofit.Builder().baseUrl(RequestUrl.getUploadUrl())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build()
.create(TjRetrofitService.class);
}
return retroUploadFileService;
}

其中TjRetrofitService :定义接口的

public interface TjRetrofitService {

/**
 * 获取系统配置
 *
 * @return
 */
@GET("tjs/pad/tjs/sys/getConfig")
Observable> mGetConfig();

}

你可能感兴趣的:(Android upload 文件回调进度)