List<File> imageLists = new ArrayList<>(); File photo1file = new File(getContext().getExternalFilesDir("photos").getPath(), photo1.toFileName());
File photo2file = new File(getContext().getExternalFilesDir("photos").getPath(), photo2.toFileName());
private static final MediaType MEDIA_TYPE_IAMGE = MediaType.parse("image/*"); /** * okHttp post异步请求表单提交 * * @param actionUrl 接口地址 * @param paramsMap 请求参数 * @param fileParamName 图片参数的名字 * @param imageLists 图片参数的列表 * @param callBack 请求返回数据回调 * @param <T> 数据泛型 * @return */ public <T> Call requestPostByAsynWithImageList(String actionUrl, HashMap<String, String> paramsMap, String fileParamName, List<File> imageLists, final ReqCallBack<T> callBack) { try { MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM); for (String key : paramsMap.keySet()) {// builder.add(key, paramsMap.get(key)); builder.addFormDataPart(key,paramsMap.get(key)); } for(File image : imageLists) { builder.addFormDataPart(fileParamName, image.getName(), RequestBody.create(MEDIA_TYPE_IAMGE, image)); } RequestBody formBody = builder.build(); String requestUrl = String.format("%s/%s", WebUrls.baseurl, actionUrl); final Request request = addHeaders().url(requestUrl).post(formBody).build(); final Call call = mOkHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { failedCallBack("访问失败", callBack); Log.e(TAG, e.toString()); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String string = response.body().string(); String header=response.header("Content-MD5"); Log.e(TAG, "response ----->" + string); successCallBack((T) string,header, callBack); } else { failedCallBack("服务器错误", callBack); } } }); return call; } catch (Exception e) { Log.e(TAG, e.toString()); } return null; }