最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用Retrofit上傳文件功能

 更新時間:2022年01月17日 11:38:19   作者:最萌小熊貓  
這篇文章主要為大家詳細(xì)介紹了Android使用Retrofit上傳文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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();
? ? ? ? ? ? // 攔截等級為body(可以打印出完整的網(wǎng)絡(luò)請求)
? ? ? ? ? ? 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.實例化api接口

// 實例化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.提交請求

api.upload(parts)
? ?.observeOn(AndroidSchedulers.mainThread())
? ?.subscribeOn(Schedulers.io())
? ?.subscribe(new Observer<String>() {
? ? ? ?@Override
? ? ? ?public void onNext(String s) {
? ? ? ? ? ?Log.i("--",s); // 請求結(jié)果
? ? ? ?}
? ? ? ?@Override
? ? ? ?public void onError(Throwable e) {

? ? ? ?}
? ? ? ?@Override
? ? ? ?public void onComplete() {

? ? ? ?}
? ?});

三、上傳多個文件

1.在Api接口中聲明方法

@Multipart
@POST("fileUploadMore")
Observable<String> uploadMore(@PartMap Map<String, List<MultipartBody.Part>> multiMap);

2.實例化api接口

// 實例化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.提交請求

api.uploadMore(parts)
? ?.observeOn(AndroidSchedulers.mainThread())
? ?.subscribeOn(Schedulers.io())
? ?.subscribe(new Observer<String>() {
? ? ? ?@Override
? ? ? ?public void onNext(String s) {
? ? ? ? ? ?Log.i("--",s); // 請求結(jié)果
? ? ? ?}
? ? ? ?@Override
? ? ? ?public void onError(Throwable e) {

? ? ? ?}
? ? ? ?@Override
? ? ? ?public void onComplete() {

? ? ? ?}
? ?});

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

白城市| 大宁县| 井研县| 炎陵县| 南京市| 浦北县| 雷山县| 内乡县| 资兴市| 屏东县| 桦甸市| 宜都市| 克东县| 七台河市| 偏关县| 湖北省| 尚志市| 福鼎市| 石柱| 晋州市| 塔城市| 诸暨市| 宜宾市| 巧家县| 镇赉县| 广汉市| 上高县| 宝山区| 连山| 独山县| 满城县| 安乡县| 固镇县| 凤冈县| 宿迁市| 望谟县| 花莲县| 苗栗县| 观塘区| 明光市| 治多县|