Retrofit 上传多张图片/文件
2016-06-22 14:41
507 查看
不多说,直接上代码,感兴趣的话可以自己去看看 retrofit
接口中的方法
调用方式
接口中的方法
public interface WebApiServices { String baseUrl = ""; // baseUrl @Multipart @POST("/xxx/xxx/xxx/xxx/") //这里是Url Call<Ret> submitUserInfoChange(@PartMap Map<String, RequestBody> infoWidthPhoto); }
public class RetrofitManager { private static Retrofit retrofit = null; private static OkHttpClient client = new OkHttpClient() .newBuilder().connectTimeout(60, TimeUnit.SECONDS).build(); public static WebApiServices getWebApiService() { if (retrofit == null) { retrofit = new Retrofit.Builder() .baseUrl(WebApiServices.BASEURL) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); } return retrofit.create(WebApiServices.class); } }
调用方式
private RequestBody transformationParams(String param) { String content = TextUtils.isEmpty(param) ? "" : param; return RequestBody.create(MediaType.parse("text/plain"), content); } //提交数据 private void submitData(SubmitPostBena mBean, List<String> faile) { if (SystemUtils.checkNet(this)) { WebApiServices webApiService = RetrofitManager.getWebApiService(); Map<String, RequestBody> post = new HashMap<>(); post.put("tagIds", transformationParams(mBean.getTagIds())); List<String> imagePaths = mBean.getImagePaths(); int pathCount = imagePaths.size(); for (int i = 0; i < pathCount; i++) { File file = new File(imagePaths.get(i)); RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file); post.put("img" + i + "\"; filename=\"" + file.getName(), requestFile); } } Call<Ret> call = webApiService.submitPost(post); call.enqueue(new Callback<Ret>() { @Override public void onResponse(Call<Ret> call, Response<Ret> response) { if (response.isSuccessful()) { Ret body = response.body(); if (body.getRet().equals("1")) { } else { } } } @Override public void onFailure(Call<Ret> call, Throwable t) { LogUtil.e(TAG, t); } }); } }
相关文章推荐
- 自动共享和上传文件到兼容的托管站点
- 实现FTP整站上传的批处理代码
- asp防止上传图片木马原理解析
- 批处理向FTP上传具有指定属性的文件(增量备份)
- 用PHP实现文件上传
- PHP实现多文件上传的方法
- PHP安全上传图片的方法
- php限制上传文件类型并保存上传文件的方法
- 在ASP.NET 2.0中操作数据之五十四:添加新记录时包含一个文件上传选项
- 在ASP.NET 2.0中操作数据之五十二:使用FileUpload上传文件
- C#实现图片上传与浏览切换的方法
- PHP多个文件上传到服务器实例
- JS上传组件FileUpload自定义模板的使用方法
- 通过隐藏iframe实现无刷新上传文件操作
- js实现上传图片及时预览
- js验证上传图片的方法
- HTML5实现微信拍摄上传照片功能
- Webwork 实现文件上传下载代码详解
- 基于HTML5的可预览多图片Ajax上传
- Android WebView 上传文件支持全解析