每個(gè)應(yīng)用基本都會(huì)涉及到文件的上傳或下載叶骨,最普遍的一般也就是上傳頭像或者照片搬素,下載安裝包了霎箍,本篇文章就這兩點(diǎn)簡(jiǎn)單說一下retrofit+rxjava的對(duì)文件的上傳和下載奇钞。
1.上傳
首先說一下單文件上傳,一般上傳頭像等會(huì)用到.
1).寫api@Multipart
@POST
("")//引號(hào)內(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ā)起請(qǐng)求
mPresenter. httpName (f);
其中f我為你要上傳的文件對(duì)象
以圖片為例,經(jīng)過裁剪后將其轉(zhuǎn)化為文件對(duì)象方法如下
@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);
}}}//括號(hào)可能多或者少,自己添吧
再說一下多文件上傳,以及附帶有參數(shù)的請(qǐng)求,類似這樣
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//下載大文件時(shí)需要加上
@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對(duì)象中
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ā)起下載請(qǐng)求
mPresenter.httpToDownload(downPath, file);//file為你下載下來的文件要存放的位置
因本人app中用的是rxjava1,所以一些rxjava+retrofit處理邏輯寫的不細(xì)甚至有些亂,所以大家可以自己作相應(yīng)修改,不要拿來就用.