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

使用异步httpclient框架做get,post提交数据

时间:2016-06-21 17:12:01      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

1.将异步httpclient框架导入

技术分享

下载地址:http://download.csdn.net/detail/sinat_32804317/9555641

 

2.代码实现

 1 public class MainActivity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7     }
 8 
 9 
10     public void get(View v){
11         EditText et_name = (EditText) findViewById(R.id.et_name);
12         EditText et_pass = (EditText) findViewById(R.id.et_pass);
13         
14         final String name = et_name.getText().toString();
15         final String pass = et_pass.getText().toString();
16         String url = "http://192.168.13.13/Web/servlet/CheckLogin?name=" + URLEncoder.encode(name) + "&pass=" + pass;
17         //创建异步httpclient
18         AsyncHttpClient ahc = new AsyncHttpClient();
19         
20         //发送get请求提交数据
21         ahc.get(url, new MyResponseHandler());
22     }
23     
24     public void post(View v){
25         EditText et_name = (EditText) findViewById(R.id.et_name);
26         EditText et_pass = (EditText) findViewById(R.id.et_pass);
27         
28         final String name = et_name.getText().toString();
29         final String pass = et_pass.getText().toString();
30         String url = "http://192.168.13.13/Web/servlet/CheckLogin";
31         
32         //创建异步httpclient
33         AsyncHttpClient ahc = new AsyncHttpClient();
34         
35         //发送post请求提交数据
36         //把要提交的数据封装至RequestParams对象
37         RequestParams params = new RequestParams();
38         params.add("name", name);
39         params.add("pass", pass);
40         ahc.post(url, params, new MyResponseHandler());
41     }
42     
43     class MyResponseHandler extends AsyncHttpResponseHandler{
44 
45         //请求服务器成功时,此方法调用
46         @Override
47         public void onSuccess(int statusCode, Header[] headers,
48                 byte[] responseBody) {
49             Toast.makeText(MainActivity.this, new String(responseBody), 0).show();
50             
51         }
52 
53         //请求失败此方法调用
54         @Override
55         public void onFailure(int statusCode, Header[] headers,
56                 byte[] responseBody, Throwable error) {
57             Toast.makeText(MainActivity.this, "请求失败", 0).show();
58             
59         }
60         
61     }
62     
63 }

 详细介绍:http://www.open-open.com/lib/view/open1352201229000.html

使用异步httpclient框架做get,post提交数据

标签:

原文地址:http://www.cnblogs.com/wangying222/p/5604071.html

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