RxJava+Retrofit+OkHttp實現(xiàn)文件上傳
背景
在實際運用中上傳是一個必不可少的功能,所以我們在封裝二的基礎(chǔ)上加入上傳的功能,同時需要附帶上傳進度!
效果

實現(xiàn)
1.定義service接口
注意:Multipart是指定大文件上傳過程中的標示,一般上傳圖片的過程中我們需要附帶信息,所以我們需要用到@part指定傳遞的數(shù)值,MultipartBody.Part是指定傳遞的文件;
/*上傳文件*/
@Multipart
@POST("AppYuFaKu/uploadHeadImg")
Observable<BaseResultEntity<UploadResulte>> uploadImage(@Part("uid") RequestBody uid, @Part("auth_key") RequestBody auth_key,@Part MultipartBody.Part file);
2.加入進度條
retrofit是基于okhttp的處理,所以我們可以自定義RequestBody,復(fù)寫writeTo(BufferedSink sink)方法,得到傳遞的進度數(shù)據(jù)
public class ProgressRequestBody extends RequestBody {
//實際的待包裝請求體
private final RequestBody requestBody;
//進度回調(diào)接口
private final UploadProgressListener progressListener;
//包裝完成的BufferedSink
private BufferedSink bufferedSink;
public ProgressRequestBody(RequestBody requestBody, UploadProgressListener progressListener) {
this.requestBody = requestBody;
this.progressListener = progressListener;
}
/**
* 重寫調(diào)用實際的響應(yīng)體的contentType
* @return MediaType
*/
@Override
public MediaType contentType() {
return requestBody.contentType();
}
/**
* 重寫調(diào)用實際的響應(yīng)體的contentLength
* @return contentLength
* @throws IOException 異常
*/
@Override
public long contentLength() throws IOException {
return requestBody.contentLength();
}
/**
* 重寫進行寫入
* @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,否則最后一部分數(shù)據(jù)可能不會被寫入
bufferedSink.flush();
}
/**
* 寫入,回調(diào)進度接口
* @param sink Sink
* @return Sink
*/
private Sink sink(Sink sink) {
return new ForwardingSink(sink) {
//當前寫入字節(jié)數(shù)
long writtenBytesCount = 0L;
//總字節(jié)長度,避免多次調(diào)用contentLength()方法
long totalBytesCount = 0L;
@Override
public void write(Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);
//增加當前寫入的字節(jié)數(shù)
writtenBytesCount += byteCount;
//獲得contentLength的值,后續(xù)不再調(diào)用
if (totalBytesCount == 0) {
totalBytesCount = contentLength();
}
Observable.just(writtenBytesCount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
progressListener.onProgress(writtenBytesCount, totalBytesCount);
}
});
}
};
}
}
3自定義接口,回調(diào)progress進度
public interface UploadProgressListener {
/**
* 上傳進度
* @param currentBytesCount
* @param totalBytesCount
*/
void onProgress(long currentBytesCount, long totalBytesCount);
}
4創(chuàng)建RequestBody對象,加入進度
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請求的方式一樣,我們需要繼承baseentity,復(fù)寫里面的方法,然后設(shè)置需要傳遞的參數(shù),因為是測試接口,所以我的參數(shù)直接寫死在entity里面,part文件動態(tài)指定
/**
* 上傳請求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請求處理
請求和封裝二中的請求一樣,通過傳遞一個指定的HttpOnNextListener 對象來回調(diào)來監(jiān)聽結(jié)果信息,一一對應(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<UploadResulte>() {
@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());
}
};
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?WebView緩存機制優(yōu)化加載慢問題
我知道你一定在煩惱Android?Webview的性能問題,特別突出的是-加載速度慢、消耗流量,針對Android?Webview的性能問題,提出一些有效解決方案2023-02-02
Android使用SqLite實現(xiàn)登錄注冊功能流程詳解
這篇文章主要介紹了使用Android Studio自帶的sqlite數(shù)據(jù)庫實現(xiàn)一個簡單的登錄注冊功能,SQLite是一個軟件庫,實現(xiàn)了自給自足的、無服務(wù)器的、零配置的、事務(wù)性的SQL數(shù)據(jù)庫引擎,本文給大家介紹的非常詳細,需要的朋友可以參考下2023-12-12
Android使用WebView實現(xiàn)全屏切換播放網(wǎng)頁視頻功能
這篇文章主要介紹了Android使用WebView實現(xiàn)全屏切換播放網(wǎng)頁視頻功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2019-07-07
Android BottomNavigationBar底部導(dǎo)航控制器使用方法詳解
這篇文章主要為大家詳細介紹了Android BottomNavigationBar底部導(dǎo)航控制器使用方法,感興趣的小伙伴們可以參考一下2016-03-03
PowerManagerService之手動滅屏流程示例分析
這篇文章主要為大家介紹了PowerManagerService之手動滅屏流程的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Android開發(fā)入門之Notification用法分析
這篇文章主要介紹了Android中Notification用法,較為詳細的分析了Notification的功能、使用步驟與相關(guān)注意事項,需要的朋友可以參考下2016-07-07
Android 啟動activity的4種方式及打開其他應(yīng)用的activity的坑
這篇文章主要介紹了Android 啟動activity的4種方式及打開其他應(yīng)用的activity的坑的相關(guān)資料,需要的朋友可以參考下2016-05-05

