Android使用Retrofit上傳文件功能
本文實(shí)例為大家分享了Android使用Retrofit上傳文件的具體代碼,供大家參考,具體內(nèi)容如下
一、封裝RetrofitManager
public class RetrofitManager { ? ? private static RetrofitManager retrofitManager; ? ?? ? ? private Retrofit retrofit; ? ? private RetrofitManager() {} ? ? public static RetrofitManager getInstance() { ? ? ? ? if (retrofitManager == null) { ? ? ? ? ? ? synchronized (RetrofitManager.class) { ? ? ? ? ? ? ? ? if (retrofitManager == null) { ? ? ? ? ? ? ? ? ? ? retrofitManager = new RetrofitManager(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return retrofitManager; ? ? } ? ? public Retrofit getRetrofit() { ? ? ? ? if (retrofit == null) { ? ? ? ? ?? ?// 添加日志攔截器 ? ? ? ? ? ? HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); ? ? ? ? ? ? // 攔截等級(jí)為body(可以打印出完整的網(wǎng)絡(luò)請(qǐng)求) ? ? ? ? ? ? httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY); ?? ??? ??? ?// 使用OkHttpClient ? ? ? ? ? ? OkHttpClient okHttpClient = new OkHttpClient.Builder() ? ? ? ? ? ? ? ? ? ? .addInterceptor(httpLoggingInterceptor) ? ? ? ? ? ? ? ? ? ? .connectTimeout(1, TimeUnit.MINUTES) ? ? ? ? ? ? ? ? ? ? .readTimeout(1,TimeUnit.MINUTES) ? ? ? ? ? ? ? ? ? ? .build(); ?? ??? ??? ?// 創(chuàng)建出Retrofit ? ? ? ? ? ? retrofit = new Retrofit.Builder() ? ? ? ? ? ? ??? ??? ?// 使用Gson轉(zhuǎn)換工廠 ? ? ? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create()) ?? ??? ??? ??? ??? ?.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) ?? ??? ??? ??? ??? ?// 基礎(chǔ)Url ? ? ? ? ? ? ? ? ? ? .baseUrl("http://**.**.**.**:**/") ? ? ? ? ? ? ? ? ? ? .client(okHttpClient) ? ? ? ? ? ? ? ? ? ? .build(); ? ? ? ? } ? ? ? ? return retrofit; ? ? } }
二、上傳單一文件
1.在Api接口中聲明方法
@Multipart @POST("fileUpload") Observable<String> upload(@Part List<MultipartBody.Part> parts);
2.實(shí)例化api接口
// 實(shí)例化api接口 Api api = RetrofitManager.getInstance().getRetrofit().create(Api.class);
3.構(gòu)建參數(shù)
File file = new File("/sdcard/DCIM/Camera/**.jpg"); RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody multipartBody = new MultipartBody.Builder() ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName.jpg", body) ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM) ? ? ? ? ? ? ? ? .build();
4.提交請(qǐng)求
api.upload(parts) ? ?.observeOn(AndroidSchedulers.mainThread()) ? ?.subscribeOn(Schedulers.io()) ? ?.subscribe(new Observer<String>() { ? ? ? ?@Override ? ? ? ?public void onNext(String s) { ? ? ? ? ? ?Log.i("--",s); // 請(qǐng)求結(jié)果 ? ? ? ?} ? ? ? ?@Override ? ? ? ?public void onError(Throwable e) { ? ? ? ?} ? ? ? ?@Override ? ? ? ?public void onComplete() { ? ? ? ?} ? ?});
三、上傳多個(gè)文件
1.在Api接口中聲明方法
@Multipart @POST("fileUploadMore") Observable<String> uploadMore(@PartMap Map<String, List<MultipartBody.Part>> multiMap);
2.實(shí)例化api接口
// 實(shí)例化api接口 Api api = RetrofitManager.getInstance().getRetrofit().create(Api.class);
3.構(gòu)建參數(shù)
File file = new File("/sdcard/DCIM/Camera/**.jpg"); RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file); MultipartBody multipartBody1 = new MultipartBody.Builder() ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName1.jpg", body) ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM) ? ? ? ? ? ? ? ? .build(); MultipartBody multipartBody2 = new MultipartBody.Builder() ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName2.jpg", body) ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM) ? ? ? ? ? ? ? ? .build(); MultipartBody multipartBody3 = new MultipartBody.Builder() ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName3.jpg", body) ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM) ? ? ? ? ? ? ? ? .build(); MultipartBody multipartBody4 = new MultipartBody.Builder() ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName4.jpg", body) ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM) ? ? ? ? ? ? ? ? .build(); // 把所有文件放入map集合中 Map<String, List<MultipartBody.Part>> parts = new HashMap<>(); parts.put("f1",multipartBody1.parts()); parts.put("f2",multipartBody2.parts()); parts.put("f3",multipartBody3.parts()); parts.put("f4",multipartBody4.parts());
4.提交請(qǐng)求
api.uploadMore(parts) ? ?.observeOn(AndroidSchedulers.mainThread()) ? ?.subscribeOn(Schedulers.io()) ? ?.subscribe(new Observer<String>() { ? ? ? ?@Override ? ? ? ?public void onNext(String s) { ? ? ? ? ? ?Log.i("--",s); // 請(qǐng)求結(jié)果 ? ? ? ?} ? ? ? ?@Override ? ? ? ?public void onError(Throwable e) { ? ? ? ?} ? ? ? ?@Override ? ? ? ?public void onComplete() { ? ? ? ?} ? ?});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android監(jiān)聽(tīng)器實(shí)例代碼
在本篇中小編給大家整理了一篇關(guān)于android監(jiān)聽(tīng)器的相關(guān)知識(shí)點(diǎn)文章,需要的朋友們可以學(xué)習(xí)下。2019-10-10Android sd卡讀取數(shù)據(jù)庫(kù)實(shí)例代碼
這篇文章主要介紹了Android sd卡讀取數(shù)據(jù)庫(kù)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02TextInputLayout輸入框控件的懸浮標(biāo)簽
這篇文章主要為大家詳細(xì)介紹了TextInputLayout輸入框控件的懸浮標(biāo)簽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android 判斷SIM卡是中國(guó)移動(dòng)\中國(guó)聯(lián)通\中國(guó)電信(移動(dòng)運(yùn)營(yíng)商)
本文給帶來(lái)兩種方法來(lái)判斷sim卡是屬于哪個(gè)運(yùn)營(yíng)商的,要實(shí)現(xiàn)此功能我們需要先獲取手機(jī)的imsi碼然后在判斷,對(duì)此功能感興趣的朋友一起通過(guò)本文學(xué)習(xí)吧2016-09-09基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳
這篇文章主要介紹了基于標(biāo)準(zhǔn)http實(shí)現(xiàn)Android多文件上傳的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01android viewpager實(shí)現(xiàn)豎屏滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了android viewpager實(shí)現(xiàn)豎屏滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android自定義View實(shí)現(xiàn)圓環(huán)交替效果
這篇文章給大家介紹如何基于Android自定義View實(shí)現(xiàn)圓環(huán)交替的效果,實(shí)現(xiàn)后效果很贊,有需要的小伙伴們可以參考借鑒。2016-08-08android中選中菜單的顯示跳轉(zhuǎn)和隱式跳轉(zhuǎn)的實(shí)例介紹
android中選中菜單的顯示跳轉(zhuǎn)和隱式跳轉(zhuǎn)的實(shí)例介紹,需要的朋友可以參考一下2013-05-05Android NDK中socket的用法以及注意事項(xiàng)分析
本篇文章是對(duì)Android NDK中socket的用法以及注意事項(xiàng)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06