HttpClient的post請求和get請求

來源:互聯網
上載者:User

標籤:pre   eval   mha   .exe   color   err   arraylist   get請求   client   

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                 //得到返回實體類HttpEntity12                 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                 //得到返回實體類HttpEntity19                 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請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.