序
前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示。
文件上传
定义接口
1 2 3
| @Multipart @POST("fileService") Call<User> uploadFile(@Part MultipartBody.Part file);
|
构造请求体上传
1 2 3 4 5
| File file = new File(filePath); RequestBody body = RequestBody.create(MediaType.parse("application/otcet-stream"), file); MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body); Call<User> call = getRetrofitService().uploadOneFile(part); call.enqueue(callback);
|
这样就可以将这个文件上传到服务器,但就这样上传操作不够友好,最好加上文件上传进度。而Retrofit本身是不支持文件上传进度显示的,所以就需要我们自己扩展OkHttp来实现文件上传进度。
我的做法是直接扩展一个RequestBody来实现进度显示,实现完成之后只需要将上面body进行包装转换即可
上传进度显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| RetrofitCallback<User> callback = new RetrofitCallback<User>() { @Override public void onSuccess(Call<User> call, Response<User> response) {
runOnUIThread(activity, response.body().toString()); }
@Override public void onFailure(Call<User> call, Throwable t) {
runOnUIThread(activity, t.getMessage()); }
@Override public void onLoading(long total, long progress) {
super.onLoading(total, progress); } };
RequestBody body1 = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
FileRequestBody body = new FileRequestBody(body1, callback);
MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body); Call<User> call = getRetrofitService().uploadOneFile(part); call.enqueue(callback);
|
回调RetrofitCallback
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public abstract class RetrofitCallback<T> implements Callback<T> {
@Override public void onResponse(Call<T> call, Response<T> response) {
if(response.isSuccessful()) { onSuccess(call, response); } else { onFailure(call, new Throwable(response.message())); } }
public abstract void onSuccess(Call<T> call, Response<T> response);
public void onLoading(long total, long progress) {
} }
|
FileRequestBody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
public final class FileRequestBody<T> extends RequestBody {
private RequestBody requestBody;
private RetrofitCallback<T> callback;
private BufferedSink bufferedSink;
public FileRequestBody(RequestBody requestBody, RetrofitCallback<T> callback) { super(); this.requestBody = requestBody; this.callback = callback; }
@Override public long contentLength() throws IOException { return requestBody.contentLength(); }
@Override public MediaType contentType() { return requestBody.contentType(); }
@Override public void writeTo(BufferedSink sink) throws IOException { if (bufferedSink == null) { bufferedSink = Okio.buffer(sink(sink)); } requestBody.writeTo(bufferedSink); bufferedSink.flush(); }
private Sink sink(Sink sink) { return new ForwardingSink(sink) { long bytesWritten = 0L; long contentLength = 0L;
@Override public void write(Buffer source, long byteCount) throws IOException { super.write(source, byteCount); if (contentLength == 0) { contentLength = contentLength(); } bytesWritten += byteCount; callback.onLoading(contentLength, bytesWritten); } }; } }
|
文件下载
接口定义
文件下载请求与普通的Get和Post请求是一样的,只是他们的返回值不一样而已,文件下载请求的返回值一般定义成ResponseBody
1 2 3 4
| @FormUrlEncoded @POST("fileService") Call<ResponseBody> downloadFile(@Field("param") String param);
|
发起请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| RetrofitCallback<ResponseBody> callback = new RetrofitCallback<ResponseBody>() { @Override public void onSuccess(Call<ResponseBody> call, Response<ResponseBody> response) {
try { InputStream is = response.body().byteStream(); String path = Util.getSdCardPath(); File file = new File(path, "download.jpg"); FileOutputStream fos = new FileOutputStream(file); BufferedInputStream bis = new BufferedInputStream(is); byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.flush(); fos.close(); bis.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } }
@Override public void onFailure(Call<ResponseBody> call, Throwable t) {
runOnUIThread(activity, t.getMessage()); } }; Call<ResponseBody> call = getRetrofitService(callback).downloadFile(param); call.enqueue(callback);
|
下载进度显示
下载进度显示有两种方式实现,一种是通过OkHttp设置拦截器将ResponseBody进行转换成我们扩展后的ResponseBody(稍后介绍),另外一种则是在上面的回调Callback中将ResponseBody的流写入到文件时进行进度处理,下面分别进行介绍。
扩展ResponseBody设置OkHttp拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private <T> RetrofitService getRetrofitService(final RetrofitCallback<T> callback) { OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); clientBuilder.addInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response response = chain.proceed(chain.request()); return response.newBuilder().body(new FileResponseBody<T>(response.body(), callback)).build(); } }); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(clientBuilder.build()) .addConverterFactory(GsonConverterFactory.create()) .build();
RetrofitService service = retrofit.create(RetrofitService.class); return service ; }
|
FileResponseBody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
public final class FileResponseBody<T> extends ResponseBody {
private ResponseBody mResponseBody;
private RetrofitCallback<T> mCallback;
private BufferedSource mBufferedSource;
public FileResponseBody(ResponseBody responseBody, RetrofitCallback<T> callback) { super(); this.mResponseBody = responseBody; this.mCallback = callback; }
@Override public BufferedSource source() {
if (mBufferedSource == null) { mBufferedSource = Okio.buffer(source(mResponseBody.source())); } return mBufferedSource; }
@Override public long contentLength() { return mResponseBody.contentLength(); }
@Override public MediaType contentType() { return mResponseBody.contentType(); }
private Source source(Source source) { return new ForwardingSource(source) { long totalBytesRead = 0L;
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead = super.read(sink, byteCount); totalBytesRead += bytesRead != -1 ? bytesRead : 0; mCallback.onLoading(mResponseBody.contentLength(), totalBytesRead); return bytesRead; } }; } }
|
直接在回调中进行进度更新
上面介绍了通过扩展ResponseBody同时设置OkHttp拦截器来实现进度条更新显示,另外也可以直接在请求回调onSuccess中将流转换成文件时实现进度更新,下面给出大致实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| RetrofitCallback<ResponseBody> callback = new RetrofitCallback<ResponseBody>() { @Override public void onSuccess(Call<ResponseBody> call, Response<ResponseBody> response) {
try { InputStream is = response.body().byteStream(); long totalLength = is.available(); String path = Util.getSdCardPath(); File file = new File(path, "download.jpg"); FileOutputStream fos = new FileOutputStream(file); BufferedInputStream bis = new BufferedInputStream(is); byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.flush(); fos.close(); bis.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } }
@Override public void onFailure(Call<ResponseBody> call, Throwable t) {
runOnUIThread(activity, t.getMessage()); } };
|
以上就是Retrofit中文件上传下载及其进度更新显示的实现,有疑问的可以加我微信和QQ与我联系,联系方式在这关于
原创文章,转载请出处注明。
下面是我的个人公众号,欢迎关注交流