標籤: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請求