使用异步httpclient框架做get,post提交数据
Posted oooohuhu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用异步httpclient框架做get,post提交数据相关的知识,希望对你有一定的参考价值。
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提交数据的主要内容,如果未能解决你的问题,请参考以下文章
测试框架httpclent 4.HttpClient Post方法实现
HttpClient替换HttpWebRequest--以GET和POST请求为例说明