Android使用Retrofit上传文件
最萌小熊猫 人气:0一、封装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() { } });
加载全部内容