Android网络编程之使用HttpClient和MultipartEntityBuilder 批量同时上传文件和文字

发布时间:2020-06-26 02:40:31 作者:hronyt
来源:网络 阅读:749

/** by keinta in China sz  email: hrony@qq.com  2016.10.18
 CN: 此JAR包里面已经集成了android http 网络请求包,也封装了 MultipartEntityBuilder 文件与文字同时上传,实现向下兼容
   你只需要添加这两个包就可以实现android 的多类型网络传输了
    Please add the  packages :
      compile files('libs/org.apache.http.legacy.jar')
    compile files('libs/httpmime-4.3.5.jar')
      compile files('libs/android_httpcore_4.3.2.jar')//  https://pan.baidu.com/s/1i5mEO53
Inside the JAR package has integrated the android HTTP request packet network, also encapsulates the upload MultipartEntityBuilder file with the text at the same time, achieve backward compatibility  
  /**  example 
     * @param context
     * @param url
     * @param filekey
     * @param params
     * @param file
     * @return 
     */
    public String postMutil(Context context, String url, String filekey, final TreeMap<String, String> params, File file) {
     //  ***省略***
//Image attaching
            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
            if (file != null && file.exists()) {
                multipartEntity.addBinaryBody(filekey, file, ContentType.MULTIPART_FORM_DATA, file.getName());
            }
//Json string attaching
            for (Map.Entry<String, String> entry : params.entrySet()) {
                multipartEntity.addPart(entry.getKey(), new StringBody(entry.getValue()));
            }
            httppost.setEntity(multipartEntity.build());
     //   ******省略******

    }


compile files('libs/android_httpcore_4.3.2.jar')//  https://pan.baidu.com/s/1i5mEO53   包已经上传到百度云可以直接下载用


推荐阅读:
  1. 使用php+WebUploader图片批量上传的方法
  2. jQuery无刷新上传之uploadify的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android art

上一篇:openstack学习-网络管理

下一篇:Exchange 2016邮箱导入pst状态查看

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》