• 上传多张图片文件到服务器


      
    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;
    }
  • 相关阅读:
    [转]Android Uri Intent 用法汇总
    [书目20120607]编写高质量代码:改善C#程序的157个建议
    [转]Android多媒体:实现图像的编辑和合成
    [转]Android IPC进程通信——Messager方式
    [转]Android中程序与Service交互的方式——交互方式
    [书目20120605]人力资源管理 余凯成
    [转]SurfaceView horizontal scrolling
    住房乃生活所需
    [转]android service 学习(上) 音乐播放
    [转]Android实现获取本机中所有图片
  • 原文地址:https://www.cnblogs.com/dmrbell/p/11322021.html
Copyright © 2020-2023  润新知