HttpClient的post请求和get请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient的post请求和get请求相关的知识,希望对你有一定的参考价值。

handler

 1     private int page = 1;
 2 
 3       private Handler mHandler=new Handler(){
 4               @Override
 5               public void handleMessage(Message msg) {
 6                   page++;
 7                   String result = msg.obj.toString();
 8                   Data data =new Gson().fromJson(result,Data.class);
 9                   list.addAll(data.getTngou());
10                   adapter.notifyDataSetChanged();
11               }
12           };

 

get请求

 1     private void get() {
 2         String urlPath =  "http://www.tngou.net/api/cook/list?rows=10&page=";
 3         //创建httpClient
 4              client =new DefaultHttpClient();
 5         //创建httpGet
 6         HttpGet httpGet=new HttpGet(urlPath+page);
 7         try {
 8             //client执行execute方法
 9             HttpResponse httpResponse=client.execute(    );
10             if(httpResponse.getStatusLine().getStatusCode()==200){
11                 //得到返回实体类HttpEntity
12                 HttpEntity entity = httpResponse.getEntity();
13                 //EntityUtils转换成字符串 -
14                 String result = EntityUtils.toString(entity);
15                 Log.e("get", "get: "+result);
16                 if(result!=null){
17                    Message msg=Message.obtain();
18                    msg.what=1;
19                    msg.obj=result;
20                    mHandler.sendMessage(msg);
21                 }
22             }
23         } catch (IOException e) {
24             e.printStackTrace();
25         }
26     }

 


post请求

 1   private void post() {
 2         String urlPath =  "http://www.tngou.net/api/cook/list";
 3         HttpClient client=new DefaultHttpClient();
 4         HttpPost post=new HttpPost(urlPath);
 5 
 6         BasicNameValuePair nameValuePairPage=new BasicNameValuePair("page","1");
 7         BasicNameValuePair nameValuePairRRows=new BasicNameValuePair("rows","10");
 8 
 9         try {
10             List<NameValuePair> list=new ArrayList<NameValuePair>();
11             list.add(nameValuePairPage);
12             list.add(nameValuePairRRows);
13 
14             UrlEncodedFormEntity urlEntity=new UrlEncodedFormEntity(list,"utf-8");
15             post.setEntity(urlEntity);
16             HttpResponse response=client.execute(post);
17             if(response.getStatusLine().getStatusCode()==200){
18                 //得到返回实体类HttpEntity
19                 HttpEntity entity = response.getEntity();
20                 //EntityUtils转换成字符串
21                 String result = EntityUtils.toString(entity);
22                 Log.e("post", "post: "+result);
23                 if(result!=null){
24                     Message msg=Message.obtain();
25                     msg.what=1;
26                     msg.obj=result;
27                     mHandler.sendMessage(msg);
28                 }
29             }
30         } catch (Exception e) {
31             e.printStackTrace();
32         }
33 
34 
35 
36     }

 

以上是关于HttpClient的post请求和get请求的主要内容,如果未能解决你的问题,请参考以下文章

Java学习心得之 HttpClient的GET和POST请求

C#用HttpClient类post get,怎么设置cookies

HttpClient的post请求和get请求

httpclient 中post请求重定向

HttpClient GET和POST请求

HttpClient和AsynchttpClient的get与post请求方式