Retrofit2 file upload download and its progress display

Source: Internet
Author: User
Tags getmessage

Order

The previous article introduced the basic use of Retrofit2, which then introduced the use of Retrofit2 to implement file uploads and file downloads, as well as how to achieve progress during the upload download process.

File Upload Definition interface
123 @Multipart @POST ("Fileservice") call<user> uploadfile(@Part multipartbody.part file);
Construct the request body upload
12345 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);

This can be uploaded to the server, but the upload operation is not friendly, it is best to add the file upload progress. and retrofit itself is not support file upload progress display, so we need to expand the okhttp to achieve file upload progress.

My practice is to directly expand a requestbody to achieve the progress of the display, after the completion of the implementation of the body only need to wrap the conversion can

Upload Progress Display
12345678910111213141516171819202122232425262728293031 Retrofitcallback<user> callback =New Retrofitcallback<user> () {@Overridepublic void onsuccess(call<user> call, response<user> Response) { Runonuithread (activity, Response.body (). toString ());//Progress update ended}@Overridepublic void onfailure(call<user> call, Throwable t) { Runonuithread (activity, t.getmessage ());//Progress update ended}@Overridepublic void onloading(long total, long progress) { super.onloading (total, progress);//Progress updates here}}; Requestbody body1 = requestbody.create (Mediatype.parse ("Application/otcet-stream"), file);//Convert Requestbody to a specific filerequestbody through this line of codeFilerequestbody BODY =New Filerequestbody (Body1, callback);Multipartbody.part part = MultipartBody.Part.createFormData ("File", File.getname (), body);Call<user> call = Getretrofitservice (). Uploadonefile (part); Call.enqueue (callback);
Callback Retrofitcallback
123456789101112131415161718 Public Abstract class retrofitcallback<t> implements callback<t> { @Overridepublic 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374 /*** Expand the Okhttp request body, implement the progress prompt when uploading * *@param <T>*/Public Final class filerequestbody<t> extends requestbody { /*** Actual request Body */private requestbody requestbody;/*** Upload Callback interface */private retrofitcallback<t> callback;/*** Packaging completed bufferedsink*/private Bufferedsink Bufferedsink;public filerequestbody(requestbody requestbody, retrofitcallback<t> callback) { super ();this.requestbody = requestbody;this.callback = callback;}@Overridepublic Long contentlength() throws IOException { return Requestbody.contentlength ();}@OverridePublic mediatype ContentType() { return Requestbody.contenttype ();}@Overridepublic void writeto(bufferedsink sink) throws IOException { if (Bufferedsink = = null) { //PackagingBufferedsink = Okio.buffer (sink (sink));}//writeRequestbody.writeto (Bufferedsink);//must call flush, otherwise the last part of the data may not be writtenBufferedsink.flush ();}/*** Write, Callback Progress interface *@param sink Sink*@return Sink*/private Sink Sink(Sink Sink) { return new Forwardingsink (sink) { //Number of bytes currently writtenlong byteswritten = 0L; //Total byte length, avoid multiple calls to the ContentLength () methodlong contentlength = 0L; @Overridepublic void Write(Buffer source, long ByteCount) throws IOException { /c16> super.write (source, byteCount);if (contentlength = = 0) { //Get the value of contentlength, follow it no longer callContentLength = ContentLength ();}//Increase the number of bytes currently writtenByteswritten + = ByteCount;//CallbackCallback.onloading (ContentLength, Byteswritten);}};}
File Download Interface definition

The file download request is the same as the normal get and post requests, except that their return value is different, and the return value of the file download request is generally defined asResponseBody

1234 //Here Only example post method for file download @FormUrlEncoded @POST ("Fileservice") call<responsebody> downloadFile(@Field ("param") String param);
Initiating a request
123456789101112131415161718192021222324252627282930313233 Retrofitcallback<responsebody> callback =New Retrofitcallback<responsebody> () {@Overridepublic 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[[1024x768]; 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 ();}}@Overridepublic void onfailure(call<responsebody> call, Throwable t) { Runonuithread (activity, t.getmessage ());}; Call<responsebody> call = Getretrofitservice (callback). DownloadFile (param); Call.enqueue (callback);
Download Progress display

The download progress display is implemented in two ways, one of which is to convert the Responsebody to our extended responsebody (described later) through the Okhttp Setup interceptor. The other is the progress of the Responsebody stream written to the file in the callback callback above, as described below.

Extended Responsebody Setting Okhttp Interceptor
12345678910111213141516171819202122 private <T> retrofitservice getretrofitservice(final retrofitcallback<t> callback) { Okhttpclient.builder Clientbuilder =new Okhttpclient.builder ();Clientbuilder.addinterceptor (New Interceptor () {@OverridePublic OKHTTP3. Response intercept(Chain Chain) throws IOException { OKHTTP3. Response Response = Chain.proceed (Chain.request ());//Convert responsebody into the fileresponsebody we needreturn 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;}//Through the above settings, we need to implement the Onloading method in callback Retrofitcallback to make progress update operation, same as the method of uploading the file
Fileresponsebody
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 /*** Expand the Okhttp request body, implement the progress prompt when uploading * *@param <T>*/Public Final class fileresponsebody<t> extends responsebody { /*** Actual request Body */private responsebody mresponsebody;/*** Download Callback Interface */private retrofitcallback<t> Mcallback;/*** bufferedsource*/private Bufferedsource Mbufferedsource;public fileresponsebody(responsebody responsebody, retrofitcallback<t> callback) { super ();this.mresponsebody = responsebody;this.mcallback = callback;}@OverridePublic Bufferedsource source() { if (Mbufferedsource = = null) { Mbufferedsource = Okio.buffer (Source (Mresponsebody.source ()));}return mbufferedsource;}@Overridepublic Long contentlength() { return Mresponsebody.contentlength ();}@OverridePublic mediatype ContentType() { return Mresponsebody.contenttype ();}/*** Callback Progress Interface *@param source*@return Source*/Private Source source(source source) { return new Forwardingsource (source) { long totalbytesread = 0L; @Overridepublic 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;}};}}
Make progress updates directly in the callback

The above describes the use of extended responsebody simultaneously set okhttp Interceptor to achieve progress bar update display, in addition, you can also directly in the request callback onsuccess to change the flow into a file to achieve progress updates, the following gives the approximate implementation

12345678910111213141516171819202122232425262728293031323334353637 Retrofitcallback<responsebody> callback =New Retrofitcallback<responsebody> () {@Overridepublic void onsuccess(call<responsebody> call, response<responsebody> Response) { try {InputStream is = Response.body (). ByteStream ();//Get total file lengthLong 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[[1024x768]; int len;While (len = bis.read (buffer))! =-1) { Fos.write (Buffer,0, Len);//Update operation here//len can be understood as the number of bytes downloaded//onloading (len, totallength);}fos.flush (); Fos.close (); Bis.close (); Is.close ();//Here represents the end of the update}catch (IOException e) {E.printstacktrace ();}}@Overridepublic void onfailure(call<responsebody> call, Throwable t) { Runonuithread (activity, t.getmessage ());};

The above is the implementation of the file upload download and its progress update display in retrofit

Retrofit2 file upload download and its progress display

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.