android HttpClient 附带的参数
2014-07-17 12:24
183 查看
Sending images can be done using the HttpComponents libraries. Download the latest HttpClient (currently4.0.1) binary with dependencies package and copy
You will need to add the following imports to your class.
[/code]
Now you can create a
public void post(String url, List nameValuePairs) {
HttpClient
httpClient = new DefaultHttpClient();
HttpContext
localContext = new BasicHttpContext();
HttpPost
httpPost = new HttpPost(url);
try
{
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("image"))
{
// If the key equals to "image", we use FileBody to transfer the
data
entity.addPart(nameValuePairs.get(index).getName(), new
FileBody(new File (nameValuePairs.get(index).getValue())));
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(), new
StringBody(nameValuePairs.get(index).getValue()));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
} catch
(IOException e) {
e.printStackTrace();
}
}
这个方法目测写得不错,先留着用
apache-mime4j-0.6.jarand
httpmime-4.0.1.jarto your project and add them to your Java build path.
You will need to add the following imports to your class.
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody;
[/code]
Now you can create a
MultipartEntityto attach an image to your POST request. The following code shows an example of how to do this:
public void post(String url, List nameValuePairs) {
HttpClient
httpClient = new DefaultHttpClient();
HttpContext
localContext = new BasicHttpContext();
HttpPost
httpPost = new HttpPost(url);
try
{
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("image"))
{
// If the key equals to "image", we use FileBody to transfer the
data
entity.addPart(nameValuePairs.get(index).getName(), new
FileBody(new File (nameValuePairs.get(index).getValue())));
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(), new
StringBody(nameValuePairs.get(index).getValue()));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
} catch
(IOException e) {
e.printStackTrace();
}
}
这个方法目测写得不错,先留着用
相关文章推荐
- android网络通讯之HttpClient4不指定参数名发送Post
- Android http附带参数的文件上传
- [置顶] android网络通讯之HttpClient4不指定参数名发送Post
- android使用AsyncHttpClient上传图片以及参数到服务器
- android Httpclient 设置timeout
- Android HttpClient网络通信
- Android http get/post传递参数
- android httpclient
- Android HttpClient网络通信
- Android httpclient接收xml乱码问题解决方法
- Android的三种网络联接方式(URL / HttpURLConnection | HttpClient | InetAddress )
- Android 通过httpclient 调用碰到的问题总结
- Android实战之HttpClient
- android httpclient 上传文件
- Android HttpClient和URLConnection两种下载HTML源码的方法
- Android HttpClient
- Android HttpClient网络通信
- Android HttpClient && HttpPost简单使用
- Android HttpClient和URLConnection两种下载HTML源码的方法
- Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程