Android使用Retrofit2.X上传多文件和多表单示例

Android使用Retrofit2.X上传多文件和多表单示例

2023年7月25日发(作者:)

Android使⽤Retrofit2.X上传多⽂件和多表单⽰例retrofit post请求上传表单和⽂件最重要的就是就是去构建RequestBody,今天为⼤家带来⼀种超级简单的上传⽅式Body⽅式:1、Retrofit接⼝RetrofitInterface @POST(_PATH) Flowable upload(@Body RequestBody Body);2、Retrofit的配置private static RetrofitInterface getRetrofitInterface(String baseUrl) { if (retrofitInterface == null) { Retrofit retrofit = new r() .baseUrl(baseUrl) .addConverterFactory(())// .addConverterFactory(()) //string .addCallAdapterFactory(()) .build(); retrofitInterface = (); } return retrofitInterface; }3、构建body public static Flowable upload(List fileList) { //构建body//addFormDataPart()第⼀个参数为表单名字,这是和后台约定好的 r builder = new r() .setType() .addFormDataPart("username", "name")

.addFormDataPart("phone", "phone") //注意,file是后台约定的参数,如果是多图,file[],如果是单张图⽚,file就⾏ for (File file : fileList) { //这⾥上传的是多图 mDataPart("file[]", e(), (("image/*"), file)); } RequestBody requestBody = (); return getRetrofitInterface(_BASE).upload(requestBody); }4、开始执⾏上传(mUploadFileList) .subscribeOn(()) .observeOn(read()) .subscribe(new DisposableSubscriber() { @Override public void onNext(UploadImgBean uploadImgBean) { Log.i(TAG, "onNext: " );

} } @Override public void onError(Throwable throwable) { Log.i(TAG, "onError: --->" + sage()); } @Override public void onComplete() { Log.i(TAG, "onComplete: "); } });以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690216425a316164.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信