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


     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 }
  • 相关阅读:
    MySQL 基本字段类型
    《将博客搬至CSDN》
    【转载·收藏】 html5手机网站自适应需要加的meta标签
    SQL LIKE操作符 Thinkphp
    Thinkphp判断值是否为空
    Thinkphp重复字段过滤
    Thinkphp框架删除确认对话框
    PHP微信公众平台开发高级篇——群发接口(慕课网学习笔记)
    通过当前cateid来判断切换tab
    js获取当前页面的url中id
  • 原文地址:https://www.cnblogs.com/my334420/p/6764492.html
Copyright © 2020-2023  润新知