作者showsky (踏實的活著)
看板AndroidDev
標題Re: [問題] httppost
時間Tue Dec 25 00:13:17 2012
可以使用 MultipartEntity 就可以 POST file
不過 android 內建的 httpclient 少 httpmime-x.x.x.jar
自行到 apache httpclient 官方網站下載
HttpPost post = new HttpPost();
MultipartEntity multiPartEntity = new MultipartEntity();
StringBody password = new StringBody("password", Charset.forName(HTTP.UTF_8));
StringBody username = new StringBody("username", Charset.forName(HTTP.UTF_8));
multiPartEntity.addPart("password", password);
mulitPartEntity.addPart("username", username);
multiPartEntity.addPart(new FileBody(new File("xxxxxx")));
post.setEntity(multiPartEntity);
httpclient.execute(post);
細節在參考 httpclient 官方網站...
※ 引述《tsaiminghan (nahgnimiast)》之銘言:
: 請問使用httppost時針對類似php的表格,
: 有參數要填,也可以上傳本地端的檔案。
: 參數比如
: http://xxx.xxx.xx?aaa=bbb&ccc=eee
: 可是檔案怎麼一起上傳?
: 加參數有看到下面的方法
: List<NameValuePair> nameValuePairs =
: new ArrayList<NameValuePair>(valuepairs.size());
: nameValuePairs.add(new BasicNameValuePair(XXX_KEY1, XXX_VALUE1));
: nameValuePairs.add(new BasicNameValuePair(XXX_KEY2, XXX_VALUE2));
: httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
: 檔案有看到
: fileInputStream = new FileInputStream(file);
: InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream,
: file.length());
: httppost.setEntity(reqEntity);
: reqEntity.setContentType("binary/octet-stream");
: httppost.setEntity(reqEntity);
: 不過這兩個可以並用嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.198.250.59
※ 編輯: showsky 來自: 60.198.250.59 (12/25 00:17)
→ tsaiminghan:謝謝,我試試看 12/25 00:17
推 tomap41017:Apache HttpClient要的話建議整個換掉比較好? 01/05 15:46