您的位置:首页 > 理论基础 > 计算机网络

Android中使用HttpGet和HttpPost访问HTTP资源(二)

2014-02-08 15:25 567 查看
File file = new File("somefile.txt");

FileEntity entity = new FileEntity(file, "text/plain; charset=\"UTF-8\"");

HttpPost httppost = new HttpPost("http://localhost/action.do");

httppost.setEntity(entity);//各种Entity之FileEntity

List<NameValuePair>list=new ArrayList<NameValuePair>();

list.add(new BasicNameValuePair("name", name));

list.add(new BasicNameValuePair("pwd", pwd));

httpPost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));//各种Entity之UrlEncodedFormEntity

MultipartEntity mpEntity = new MultipartEntity();

File file = new File(fileurl);

mpEntity.addPart("file", newFileBody(file));//可用于自制文件的上传

mpEntity.addPart("data", new StringBody(data, Charset.forName(org.apache.http.protocol.HTTP.UTF_8)));

httpPost.setEntity(mpEntity);//各种Entity之MultipartEntity

----------------------------------------------

httpPost = new HttpPost(url);

......

HttpResponse response = client.execute(httpPost);//参数为完整地址

host = new HttpHost(domain, 80, "http");

httpPost = new HttpPost(url);

......

response = client.execute(host, httpPost);//参数拼接为完整地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: