Android網路編程之使用HttpClient和MultipartEntityBuilder 批量同時上傳檔案和文字

來源:互聯網
上載者:User

標籤:android

/** by keinta in China sz  email: [email protected]  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   包已經上傳到百度雲可以直接下載用


本文出自 “心靈捕手” 部落格,轉載請與作者聯絡!

Android網路編程之使用HttpClient和MultipartEntityBuilder 批量同時上傳檔案和文字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.