码迷,mamicode.com
首页 > 移动开发 > 详细

Android攻城狮httpClient post/get传递参数

时间:2017-04-25 22:19:30      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:服务   发送请求   state   类型   except   auto   str   传递   logs   

 

 1 public class RegistActivity extends Activity {
 2 
 3     private EditText name, age;
 4     private Button button;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         // TODO Auto-generated method stub
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.register);
11         name = (EditText) findViewById(R.id.editText2name);
12         age = (EditText) findViewById(R.id.editText1age);
13         button = (Button) findViewById(R.id.button1);
14         
15         button.setOnClickListener(new OnClickListener() {
16             
17             @Override
18             public void onClick(View v) {
19                 // TODO Auto-generated method stub
20                 String url="http://172.16.166.195:8080/web/Myservlet";
21             //    new HttpThread1(url, name.getText().toString(), age.getText().toString()).start();
22                 url = url + "?name=" +name.getText().toString() + "&age=" + age.getText().toString();
23             new HttpClientThread(url).start();//调dohttpClientGet();
24             new HttpClientThread(url,name.getText().toString(),age.getText().toString()).start();//调dohttpClientPost();
25             }
26         });
27     }
28 
29 }

 

 

 1 public class HttpClientThread extends Thread {
 2     private String url;
 3     private String name;
 4     private String age;
 5 
 6     public HttpClientThread(String url) {
 7         // TODO Auto-generated constructor stub
 8         this.url = url;
 9     }
10 
11     public HttpClientThread(String url, String name, String age) {
12         // TODO Auto-generated constructor stub
13         this.url = url;
14         this.name = name;
15         this.age = age;
16     }
17 
18     private void dohttpClientPost() {
19         HttpClient client = new DefaultHttpClient();
20         HttpPost post = new HttpPost(url);
21         // 通过NameValuePair去存储数据
22         ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
23         list.add(new BasicNameValuePair("name", name));
24         list.add(new BasicNameValuePair("age", age));
25 
26         try {
27             // 设置要发送的数据
28             post.setEntity(new UrlEncodedFormEntity(list));
29             HttpResponse response = client.execute(post);
30 
31             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
32                 String content = EntityUtils.toString(response.getEntity());
33             }
34         } catch (UnsupportedEncodingException e) {
35             // TODO Auto-generated catch block
36             e.printStackTrace();
37         } catch (ClientProtocolException e) {
38             // TODO Auto-generated catch block
39             e.printStackTrace();
40         } catch (IOException e) {
41             // TODO Auto-generated catch block
42             e.printStackTrace();
43         }
44 
45     }
46 
47     private void dohttpClientGet() {
48         // 创建HttpGet对象
49         HttpGet httpGet = new HttpGet(url);
50         // 创建Httpclient对象
51         HttpClient client = new DefaultHttpClient();
52 
53         HttpResponse response;
54 
55         try {// 发送请求
56             response = client.execute(httpGet);
57             // 判断类型
58             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
59                 // 取出服务器返回的数据
60                 String content = EntityUtils.toString(response.getEntity());
61 
62                 System.out.println("--->content" + content);
63 
64             }
65         } catch (ClientProtocolException e) {
66             // TODO Auto-generated catch block
67             e.printStackTrace();
68         } catch (IOException e) {
69             // TODO Auto-generated catch block
70             e.printStackTrace();
71         }
72 
73     }
74 
75     @Override
76     public void run() {
77         // TODO Auto-generated method stub
78         // dohttpClientGet();
79         dohttpClientPost();
80     }
81 }

 

Android攻城狮httpClient post/get传递参数

标签:服务   发送请求   state   类型   except   auto   str   传递   logs   

原文地址:http://www.cnblogs.com/my334420/p/6764492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!