溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Retrofit+Rxjava怎么實(shí)現(xiàn)文件上傳和下載功能

發(fā)布時間:2022-05-11 11:05:39 來源:億速云 閱讀:331 作者:iii 欄目:大數(shù)據(jù)

今天小編給大家分享一下Retrofit+Rxjava怎么實(shí)現(xiàn)文件上傳和下載功能的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

Retrofit簡介:

在Android API4.4之后,Google官方使用了square公司推出的okHttp替換了HttpClient的請求方式。后來square公司又推出了基于okHttp的網(wǎng)絡(luò)請求框架:Retrofit。

什么是 RxJava?

RxJava 是一個響應(yīng)式編程框架,采用觀察者設(shè)計模式。所以自然少不了 Observable 和 Subscriber 這兩個東東了。

還有一個RxAndroid,用于 Android 開發(fā),添加了 Android 用的接口。

1.上傳

首先說一下單文件上傳,一般上傳頭像等會用到 .

1).寫api @Multipart

@POST

( "" )//引號內(nèi)為地址Observable httpName(@PartMultipartBody.Part file);

2).寫presenter的方法

public void httpName(File file) {
RequestBody requestBody = RequestBody. create (MediaType. parse ( "image/png" ), file);
MultipartBody.Part part = MultipartBody.Part. createFormData ( "file" , file.getName() , requestBody);
Observable observable = api. httpName (part);
…rajava+retrofit處理邏輯
}

3)調(diào)用方法發(fā)起請求

mPresenter. httpName (f);

其中f我為你要上傳的文件對象

以圖片為例,經(jīng)過裁剪后將其轉(zhuǎn)化為文件對象方法如下

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
bitmap = bundle.getParcelable("data");
File f = new File(this.getFilesDir(), (new Date()).getTime() + ".png");
if (f.exists()) {f.delete();}
try {
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
f = null;
} catch (IOException e) {
e.printStackTrace();
f = null;
}
if (f != null) {
mPresenter. httpName(f);
}}}//括號可能多或者少,自己添吧

再說一下多文件上傳,以及附帶有參數(shù)的請求,類似這樣

mPresenter.httpUpLoadMore(id,phone, File1, File2, File3);
@Multipart
@POST("")
Observable<ResponseBody> httpUpLoadMore (@Query("id") String id, @Query("phone") String phone, @Part MultipartBody.Part file1, @Part MultipartBody.Part file2, @Part MultipartBody.Part file3);

這里附帶參數(shù)用@FieldMap Map maps也可以,用query好像不太恰當(dāng)

后面只需要像傳單文件一樣

RequestBody requestBody1/2/3 = RequestBody.create(MediaType.parse("image/png"), file1/2/3);;
MultipartBody.Part part1/2/3 = MultipartBody.Part.createFormData("file", file1/2/3.getName() , requestBody1/2/3);
Observable bservable= api.httpUpLoadMore(id,phone,part1,part2,part3);
……

2下載

1)寫api

@Streaming//下載大文件時需要加上
@GET
Observable > download(@Url String url);

2)Presenter方法

mApi.download (path)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.flatMap(new Func1,Observable>() {
@Override
public Observablecall(Response response) {
boolean b = writeToFile(response, file);//將返回的流轉(zhuǎn)寫入到file對象中
final Boolean aBoolean =Boolean.valueOf(b);
return Observable.create(new Observable.OnSubscribe(){
@Override
public void call(Subscriber subscriber) {
try {
subscriber.onNext(aBoolean);
subscriber.onCompleted();
} catch (Exceptione) {
subscriber.onError(ExceptionManager.handleException(e));}}});}})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1(){
@Override
public void call(Boolean bean) {}
}, new Action1(){
@Override
public void call(Throwablethrowable) {}});
[if !supportLineBreakNewLine]
[endif]
private boolean writeToFile(Responsebody,File file) {
try {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[2048];
inputStream =body.body().byteStream();
outputStream = new FileOutputStream(file);
while (true) {
int read =inputStream.read(fileReader);
if (read == -1) break;
outputStream.write(fileReader, 0, read);
}
outputStream.flush();
return true;
} catch (IOException e) {
return false;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}}
} catch (IOException e) {
return false;
}}

3)調(diào)用方法發(fā)起下載請求

mPresenter.httpToDownload(downPath, file);//file為你下載下來的文件要存放的位置

以上就是“Retrofit+Rxjava怎么實(shí)現(xiàn)文件上傳和下載功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI