[英]How can I attach name-value pairs to a MultiPart POST request?
此示例適用於其中一個,但不是兩個:
public void postData() {
//http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
File f = new File(filename);
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://url.com/script.php";
HttpPost post = new HttpPost(postURL);
// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// nameValuePairs.add(new BasicNameValuePair("project", projectName));
// nameValuePairs.add(new BasicNameValuePair("name", imageName));
// post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
FileBody bin = new FileBody(f);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bin);
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
好像當我調用setEntity()
,前一個實體被覆蓋 - 所以我可以擁有名稱值對或文件數據,但不能同時使用兩者。 你知道如何整合它們,所以我可以在上傳中使用URL參數嗎? 只需將它們附加到POST URL就不起作用了。
我也試過了
post.addHeader("project", projectName);
post.addHeader("name", imageName);
但這似乎也沒有用。
謝謝。
這似乎可以解決問題:
reqEntity.addPart("project", new StringBody(projectName));
reqEntity.addPart("name", new StringBody(imageName));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.