Re: [問題] httppost
可以使用 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)
→
12/25 00:17, , 1F
12/25 00:17, 1F
推
01/05 15:46, , 2F
01/05 15:46, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):