Android-async-http 的用法

逆水行舟用力撑,一篙松劲退千寻。这篇文章主要讲述Android-async-http 的用法相关的知识,希望能为你提供帮助。
【Android-async-http 的用法】 
1.android-async-http的Get和Post请求的使用

1 /* 2* 1.Android-async-http的Get和Post请求的使用 3*/ 4 public class MainActivity extends ActionBarActivity { 5 6@Override 7protected void onCreate(Bundle savedInstanceState) { 8super.onCreate(savedInstanceState); 9setContentView(R.layout.activity_main); 10// asynchttpGet(); 11asynchttpPost(); 12} 13 14// --------------------Post()方式请求网络--------------------------------------------- 15private void asynchttpPost() { 16 17String url = "http://apis.juhe.cn/mobile/get?"; 18AsyncHttpClient client = new AsyncHttpClient(); 19RequestParams params = new RequestParams(); 20params.put("phone", "13666666666"); 21params.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); 22client.post(url, params, new AsyncHttpResponseHandler() { 23@Override 24public void onSuccess(String content) { 25// TODO Auto-generated method stub 26super.onSuccess(content); 27Toast.makeText(MainActivity.this, content, 1).show(); 28} 29 30@Override 31public void onFailure(Throwable error) { 32// TODO Auto-generated method stub 33super.onFailure(error); 34Toast.makeText(MainActivity.this, "请求失败", 1).show(); 35} 36 37}); 38 39} 40 41// --------------------Get()方式请求网络--------------------------------------------- 42private void asynchttpGet() { 43AsyncHttpClient client = new AsyncHttpClient(); 44String url = "http://apis.juhe.cn/mobile/get?phone=13666666666& key=335adcc4e891ba4e4be6d7534fd54c5d"; 45client.get(url, new AsyncHttpResponseHandler() { 46 47@Override 48public void onSuccess(String content) { 49// TODO Auto-generated method stub 50super.onSuccess(content); 51Toast.makeText(MainActivity.this, content, 1).show(); 52} 53 54@Override 55public void onFailure(Throwable error) { 56// TODO Auto-generated method stub 57super.onFailure(error); 58Toast.makeText(MainActivity.this, "请求失败", 1).show(); 59} 60}); 61 62} 63 64 }

 
 
2.Android-async-http回调逻辑的二次封装
1 public class RequestUtils { 2 3public static AsyncHttpClient client=new AsyncHttpClient(); 4public static void ClientGet(String url, NetCallBack cb) { 5client.get( url,cb); 6} 7 8 9public static void ClientPost(String url, RequestParams params,NetCallBack cb) { 10client.post(url,params, cb); 11} 12 }

 
1 public abstract class NetCallBack extends AsyncHttpResponseHandler { 2@Override 3public void onStart() { 4// TODO Auto-generated method stub 5super.onStart(); 6 7System.out.println("---> > 请求开始,弹出进度条"); 8} 9 10@Override 11public void onSuccess(String content) { 12// TODO Auto-generated method stub 13super.onSuccess(content); 14onMySuccess(content); 15System.out.println("---> > 请求Success,隐藏进度条" + content); 16} 17 18@Override 19public void onFailure(Throwable error) { 20// TODO Auto-generated method stub 21super.onFailure(error); 22onMyFailure(error); 23System.out.println("---> > Failure"); 24} 25 26public abstract void onMySuccess(String result); 27 28public abstract void onMyFailure(Throwable error); 29 }

 
1 /* 2* 3* 2.Android-async-http回调逻辑的二次封装 4* 5* 6* */ 7 public class MainActivity extends ActionBarActivity { 8 9@Override 10protected void onCreate(Bundle savedInstanceState) { 11super.onCreate(savedInstanceState); 12setContentView(R.layout.activity_main); 13//asynchttpGet(); 14asynchttpPost(); 15} 16// --------------------Post()方式请求网络--------------------------------------------- 17private void asynchttpPost() { 18 19String url = "http://apis.juhe.cn/mobile/get?"; 20 21RequestParams params=new RequestParams(); 22params.put("phone", "13666666666"); 23params.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); 24RequestUtils.ClientPost(url, params,new NetCallBack() { 25 26@Override 27public void onMySuccess(String result) { 28// TODO Auto-generated method stub 29Toast.makeText(MainActivity.this, result, 1).show(); 30} 31 32@Override 33public void onMyFailure(Throwable error) { 34// TODO Auto-generated method stub 35Toast.makeText(MainActivity.this,"请求失败", 1).show(); 36} 37}); 38 39 40 41}

 

    推荐阅读