作者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)
1F:→ tsaiminghan:谢谢,我试试看 12/25 00:17
2F:推 tomap41017:Apache HttpClient要的话建议整个换掉比较好? 01/05 15:46