這篇文章給大家分享的是有關(guān)RxJava+Retrofit+OkHttp如何實(shí)現(xiàn)文件上傳的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司是由多位在大型網(wǎng)絡(luò)公司、廣告設(shè)計(jì)公司的優(yōu)秀設(shè)計(jì)人員和策劃人員組成的一個(gè)具有豐富經(jīng)驗(yàn)的團(tuán)隊(duì),其中包括網(wǎng)站策劃、網(wǎng)頁(yè)美工、網(wǎng)站程序員、網(wǎng)頁(yè)設(shè)計(jì)師、平面廣告設(shè)計(jì)師、網(wǎng)絡(luò)營(yíng)銷(xiāo)人員及形象策劃。承接:網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站改版、網(wǎng)頁(yè)設(shè)計(jì)制作、網(wǎng)站建設(shè)與維護(hù)、網(wǎng)絡(luò)推廣、數(shù)據(jù)庫(kù)開(kāi)發(fā),以高性價(jià)比制作企業(yè)網(wǎng)站、行業(yè)門(mén)戶平臺(tái)等全方位的服務(wù)。
效果
實(shí)現(xiàn)
1.定義service接口
注意:Multipart是指定大文件上傳過(guò)程中的標(biāo)示,一般上傳圖片的過(guò)程中我們需要附帶信息,所以我們需要用到@part指定傳遞的數(shù)值,MultipartBody.Part是指定傳遞的文件;
/*上傳文件*/ @Multipart @POST("AppYuFaKu/uploadHeadImg") Observable> uploadImage(@Part("uid") RequestBody uid, @Part("auth_key") RequestBody auth_key,@Part MultipartBody.Part file);
2.加入進(jìn)度條
retrofit是基于okhttp的處理,所以我們可以自定義RequestBody,復(fù)寫(xiě)writeTo(BufferedSink sink)方法,得到傳遞的進(jìn)度數(shù)據(jù)
public class ProgressRequestBody extends RequestBody { //實(shí)際的待包裝請(qǐng)求體 private final RequestBody requestBody; //進(jìn)度回調(diào)接口 private final UploadProgressListener progressListener; //包裝完成的BufferedSink private BufferedSink bufferedSink; public ProgressRequestBody(RequestBody requestBody, UploadProgressListener progressListener) { this.requestBody = requestBody; this.progressListener = progressListener; } /** * 重寫(xiě)調(diào)用實(shí)際的響應(yīng)體的contentType * @return MediaType */ @Override public MediaType contentType() { return requestBody.contentType(); } /** * 重寫(xiě)調(diào)用實(shí)際的響應(yīng)體的contentLength * @return contentLength * @throws IOException 異常 */ @Override public long contentLength() throws IOException { return requestBody.contentLength(); } /** * 重寫(xiě)進(jìn)行寫(xiě)入 * @param sink BufferedSink * @throws IOException 異常 */ @Override public void writeTo(BufferedSink sink) throws IOException { if (null == bufferedSink) { bufferedSink = Okio.buffer(sink(sink)); } requestBody.writeTo(bufferedSink); //必須調(diào)用flush,否則最后一部分?jǐn)?shù)據(jù)可能不會(huì)被寫(xiě)入 bufferedSink.flush(); } /** * 寫(xiě)入,回調(diào)進(jìn)度接口 * @param sink Sink * @return Sink */ private Sink sink(Sink sink) { return new ForwardingSink(sink) { //當(dāng)前寫(xiě)入字節(jié)數(shù) long writtenBytesCount = 0L; //總字節(jié)長(zhǎng)度,避免多次調(diào)用contentLength()方法 long totalBytesCount = 0L; @Override public void write(Buffer source, long byteCount) throws IOException { super.write(source, byteCount); //增加當(dāng)前寫(xiě)入的字節(jié)數(shù) writtenBytesCount += byteCount; //獲得contentLength的值,后續(xù)不再調(diào)用 if (totalBytesCount == 0) { totalBytesCount = contentLength(); } Observable.just(writtenBytesCount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1() { @Override public void call(Long aLong) { progressListener.onProgress(writtenBytesCount, totalBytesCount); } }); } }; } }
3自定義接口,回調(diào)progress進(jìn)度
public interface UploadProgressListener { /** * 上傳進(jìn)度 * @param currentBytesCount * @param totalBytesCount */ void onProgress(long currentBytesCount, long totalBytesCount); }
4創(chuàng)建RequestBody對(duì)象,加入進(jìn)度
File file=new File("/storage/emulated/0/Download/11.jpg"); RequestBody requestBody=RequestBody.create(MediaType.parse("image/jpeg"),file); MultipartBody.Part part= MultipartBody.Part.createFormData("file_name", file.getName(), new ProgressRequestBody(requestBody, new UploadProgressListener() { @Override public void onProgress(long currentBytesCount, long totalBytesCount) { tvMsg.setText("提示:上傳中"); progressBar.setMax((int) totalBytesCount); progressBar.setProgress((int) currentBytesCount); } }));
5.傳遞附帶信息
和封裝二中post請(qǐng)求的方式一樣,我們需要繼承baseentity,復(fù)寫(xiě)里面的方法,然后設(shè)置需要傳遞的參數(shù),因?yàn)槭菧y(cè)試接口,所以我的參數(shù)直接寫(xiě)死在entity里面,part文件動(dòng)態(tài)指定
/** * 上傳請(qǐng)求api * Created by WZG on 2016/10/20. */ public class UplaodApi extends BaseEntity { /*需要上傳的文件*/ private MultipartBody.Part part; public UplaodApi(HttpOnNextListener listener, RxAppCompatActivity rxAppCompatActivity) { super(listener, rxAppCompatActivity); setShowProgress(true); } public MultipartBody.Part getPart() { return part; } public void setPart(MultipartBody.Part part) { this.part = part; } @Override public Observable getObservable(HttpService methods) { RequestBody uid= RequestBody.create(MediaType.parse("text/plain"), "4811420"); RequestBody key = RequestBody.create(MediaType.parse("text/plain"), "21f8d9bcc50c6ac1ae1020ce12f5f5a7"); return methods.uploadImage(uid,key,getPart()); } }
6.post請(qǐng)求處理
請(qǐng)求和封裝二中的請(qǐng)求一樣,通過(guò)傳遞一個(gè)指定的HttpOnNextListener 對(duì)象來(lái)回調(diào)來(lái)監(jiān)聽(tīng)結(jié)果信息,一一對(duì)應(yīng)
private void uploadeDo(){ File file=new File("/storage/emulated/0/Download/11.jpg"); RequestBody requestBody=RequestBody.create(MediaType.parse("image/jpeg"),file); MultipartBody.Part part= MultipartBody.Part.createFormData("file_name", file.getName(), new ProgressRequestBody(requestBody, new UploadProgressListener() { @Override public void onProgress(long currentBytesCount, long totalBytesCount) { tvMsg.setText("提示:上傳中"); progressBar.setMax((int) totalBytesCount); progressBar.setProgress((int) currentBytesCount); } })); UplaodApi uplaodApi = new UplaodApi(httpOnNextListener,this); uplaodApi.setPart(part); HttpManager manager = HttpManager.getInstance(); manager.doHttpDeal(uplaodApi); } /** * 上傳回調(diào) */ HttpOnNextListener httpOnNextListener=new HttpOnNextListener() { @Override public void onNext(UploadResulte o) { tvMsg.setText("成功"); Glide.with(MainActivity.this).load(o.getHeadImgUrl()).skipMemoryCache(true).into(img); } @Override public void onError(Throwable e) { super.onError(e); tvMsg.setText("失敗:"+e.toString()); } };
感謝各位的閱讀!關(guān)于“RxJava+Retrofit+OkHttp如何實(shí)現(xiàn)文件上傳”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!