经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android使用Retrofit上传文件功能
来源:jb51  时间:2022/1/17 14:22:02  对本文有异议

本文实例为大家分享了Android使用Retrofit上传文件的具体代码,供大家参考,具体内容如下

一、封装RetrofitManager

  1. public class RetrofitManager {
  2. ? ? private static RetrofitManager retrofitManager;
  3. ? ??
  4. ? ? private Retrofit retrofit;
  5.  
  6. ? ? private RetrofitManager() {}
  7.  
  8. ? ? public static RetrofitManager getInstance() {
  9. ? ? ? ? if (retrofitManager == null) {
  10. ? ? ? ? ? ? synchronized (RetrofitManager.class) {
  11. ? ? ? ? ? ? ? ? if (retrofitManager == null) {
  12. ? ? ? ? ? ? ? ? ? ? retrofitManager = new RetrofitManager();
  13. ? ? ? ? ? ? ? ? }
  14. ? ? ? ? ? ? }
  15. ? ? ? ? }
  16. ? ? ? ? return retrofitManager;
  17. ? ? }
  18.  
  19. ? ? public Retrofit getRetrofit() {
  20. ? ? ? ? if (retrofit == null) {
  21. ? ? ? ? ?? ?// 添加日志拦截器
  22. ? ? ? ? ? ? HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
  23. ? ? ? ? ? ? // 拦截等级为body(可以打印出完整的网络请求)
  24. ? ? ? ? ? ? httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
  25. ?? ??? ??? ?// 使用OkHttpClient
  26. ? ? ? ? ? ? OkHttpClient okHttpClient = new OkHttpClient.Builder()
  27. ? ? ? ? ? ? ? ? ? ? .addInterceptor(httpLoggingInterceptor)
  28. ? ? ? ? ? ? ? ? ? ? .connectTimeout(1, TimeUnit.MINUTES)
  29. ? ? ? ? ? ? ? ? ? ? .readTimeout(1,TimeUnit.MINUTES)
  30. ? ? ? ? ? ? ? ? ? ? .build();
  31.  
  32. ?? ??? ??? ?// 创建出Retrofit
  33. ? ? ? ? ? ? retrofit = new Retrofit.Builder()
  34. ? ? ? ? ? ? ??? ??? ?// 使用Gson转换工厂
  35. ? ? ? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create())
  36. ?? ??? ??? ??? ??? ?.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  37. ?? ??? ??? ??? ??? ?// 基础Url
  38. ? ? ? ? ? ? ? ? ? ? .baseUrl("http://**.**.**.**:**/")
  39. ? ? ? ? ? ? ? ? ? ? .client(okHttpClient)
  40. ? ? ? ? ? ? ? ? ? ? .build();
  41. ? ? ? ? }
  42. ? ? ? ? return retrofit;
  43. ? ? }
  44. }

二、上传单一文件

1.在Api接口中声明方法

  1. @Multipart
  2. @POST("fileUpload")
  3. Observable<String> upload(@Part List<MultipartBody.Part> parts);

2.实例化api接口

  1. // 实例化api接口
  2. Api api = RetrofitManager.getInstance().getRetrofit().create(Api.class);

3.构建参数

  1. File file = new File("/sdcard/DCIM/Camera/**.jpg");
  2. RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
  3.  
  4. MultipartBody multipartBody = new MultipartBody.Builder()
  5. ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName.jpg", body)
  6. ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM)
  7. ? ? ? ? ? ? ? ? .build();

4.提交请求

  1. api.upload(parts)
  2. ? ?.observeOn(AndroidSchedulers.mainThread())
  3. ? ?.subscribeOn(Schedulers.io())
  4. ? ?.subscribe(new Observer<String>() {
  5. ? ? ? ?@Override
  6. ? ? ? ?public void onNext(String s) {
  7. ? ? ? ? ? ?Log.i("--",s); // 请求结果
  8. ? ? ? ?}
  9. ? ? ? ?@Override
  10. ? ? ? ?public void onError(Throwable e) {
  11.  
  12. ? ? ? ?}
  13. ? ? ? ?@Override
  14. ? ? ? ?public void onComplete() {
  15.  
  16. ? ? ? ?}
  17. ? ?});

三、上传多个文件

1.在Api接口中声明方法

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

2.实例化api接口

  1. // 实例化api接口
  2. Api api = RetrofitManager.getInstance().getRetrofit().create(Api.class);

3.构建参数

  1. File file = new File("/sdcard/DCIM/Camera/**.jpg");
  2. RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
  3.  
  4. MultipartBody multipartBody1 = new MultipartBody.Builder()
  5. ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName1.jpg", body)
  6. ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM)
  7. ? ? ? ? ? ? ? ? .build();
  8. MultipartBody multipartBody2 = new MultipartBody.Builder()
  9. ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName2.jpg", body)
  10. ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM)
  11. ? ? ? ? ? ? ? ? .build();
  12. MultipartBody multipartBody3 = new MultipartBody.Builder()
  13. ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName3.jpg", body)
  14. ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM)
  15. ? ? ? ? ? ? ? ? .build();
  16. MultipartBody multipartBody4 = new MultipartBody.Builder()
  17. ? ? ? ? ? ? ? ? .addFormDataPart("file", "fileName4.jpg", body)
  18. ? ? ? ? ? ? ? ? .setType(MultipartBody.FORM)
  19. ? ? ? ? ? ? ? ? .build();
  20.  
  21. // 把所有文件放入map集合中
  22. Map<String, List<MultipartBody.Part>> parts = new HashMap<>();
  23. parts.put("f1",multipartBody1.parts());
  24. parts.put("f2",multipartBody2.parts());
  25. parts.put("f3",multipartBody3.parts());
  26. parts.put("f4",multipartBody4.parts());

4.提交请求

  1. api.uploadMore(parts)
  2. ? ?.observeOn(AndroidSchedulers.mainThread())
  3. ? ?.subscribeOn(Schedulers.io())
  4. ? ?.subscribe(new Observer<String>() {
  5. ? ? ? ?@Override
  6. ? ? ? ?public void onNext(String s) {
  7. ? ? ? ? ? ?Log.i("--",s); // 请求结果
  8. ? ? ? ?}
  9. ? ? ? ?@Override
  10. ? ? ? ?public void onError(Throwable e) {
  11.  
  12. ? ? ? ?}
  13. ? ? ? ?@Override
  14. ? ? ? ?public void onComplete() {
  15.  
  16. ? ? ? ?}
  17. ? ?});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号