溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android網(wǎng)絡(luò)編程之使用HttpClient和MultipartEntityBuilder 批量同時(shí)上傳文件和文字

發(fā)布時(shí)間:2020-06-26 02:40:31 來(lái)源:網(wǎng)絡(luò) 閱讀:753 作者:hronyt 欄目:移動(dòng)開(kāi)發(fā)

/** by keinta in China sz  email: hrony@qq.com  2016.10.18
 CN: 此JAR包里面已經(jīng)集成了android http 網(wǎng)絡(luò)請(qǐng)求包,也封裝了 MultipartEntityBuilder 文件與文字同時(shí)上傳,實(shí)現(xiàn)向下兼容
   你只需要添加這兩個(gè)包就可以實(shí)現(xiàn)android 的多類(lèi)型網(wǎng)絡(luò)傳輸了
    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   包已經(jīng)上傳到百度云可以直接下載用


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI