網站首頁 編程語言 正文
本文實例為大家分享了Android使用Retrofit上傳文件的具體代碼,供大家參考,具體內容如下
一、封裝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(可以打印出完整的網絡請求) ? ? ? ? ? ? httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY); ?? ??? ??? ?// 使用OkHttpClient ? ? ? ? ? ? OkHttpClient okHttpClient = new OkHttpClient.Builder() ? ? ? ? ? ? ? ? ? ? .addInterceptor(httpLoggingInterceptor) ? ? ? ? ? ? ? ? ? ? .connectTimeout(1, TimeUnit.MINUTES) ? ? ? ? ? ? ? ? ? ? .readTimeout(1,TimeUnit.MINUTES) ? ? ? ? ? ? ? ? ? ? .build(); ?? ??? ??? ?// 創建出Retrofit ? ? ? ? ? ? retrofit = new Retrofit.Builder() ? ? ? ? ? ? ??? ??? ?// 使用Gson轉換工廠 ? ? ? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create()) ?? ??? ??? ??? ??? ?.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) ?? ??? ??? ??? ??? ?// 基礎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.構建參數
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); // 請求結果 ? ? ? ?} ? ? ? ?@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.構建參數
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); // 請求結果 ? ? ? ?} ? ? ? ?@Override ? ? ? ?public void onError(Throwable e) { ? ? ? ?} ? ? ? ?@Override ? ? ? ?public void onComplete() { ? ? ? ?} ? ?});
原文鏈接:https://blog.csdn.net/weixin_42600398/article/details/121554736
相關推薦
- 2022-06-14 go語言中的udp協議及TCP通訊實現示例_Golang
- 2022-07-13 python版jpeg合成pdf兩種方法
- 2023-03-22 tkinter如何實現打開文件對話框并獲取文件絕對路徑_python
- 2022-06-02 基于python的MD5腳本開發思路_python
- 2022-10-14 基于docker容器的gitlab遷移與數據恢復
- 2023-10-15 centos 切換g++
- 2022-12-24 C++中類的三種訪問權限解析:private、public與protect_C 語言
- 2023-02-12 Golang中反射的常見用法分享_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支