android学习笔记-----HttpClient

来源:互联网 发布:西部二手域名 编辑:程序博客网 时间:2024/05/16 00:59

HttpClient 接口类

DefaulthttpClient --> 继承了AbstracthttpClient --> 实现了HttpClient 



HttpClient  get 方式提交数据

//打开浏览器

HttpClient client = new DefaultHttpClient();

//输入地址

String path = "http://www.baidu.com?username="+URLEncoder.encode(username);

HttpGet  get = new HttpGet(path);

//敲回车 返回值

Httpresponse response =  client.execute(httpGet);

int code = response.getStatusLine().getStatusCode();//请求返回的状态码   200成功

InputStream is = response.getEntity.getContent();

String text = StreamTools.readInputStream(is);




HttpClient  post方式提交数据

//打开浏览器

HttpClient client = new DefaultHttpClient();


//输入地址

String path = "http://www.baidu.com”;

HttpPost  post= new HttpGet(path);


//制定要提交的数据实体

list<NameValuePair> param = new ArrayList<NameValuePaire>():

param.add(new BasicNameValuepair("key",value);

........

param.add(new BasicNameValuepair("key",value);

post.setEntity(new UriEncodedFormEntity(param,"UTF-8");

//敲回车 返回值

Httpresponse response =  client.execute(httpGet);

int code = response.getStatusLine().getStatusCode();//请求返回的状态码   200成功

InputStream is = response.getEntity.getContent();

String text = StreamTools.readInputStream(is);







a

0 0
原创粉丝点击