1.Android-async-http的Get和Post请求的使用
1 /*
2 * 1.Android-async-http的Get和Post请求的使用 3 */
4 public class MainActivity extends ActionBarActivity { 5
6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 // asynchttpGet();
11 asynchttpPost(); 12 } 13
14 // --------------------Post()方式请求网络---------------------------------------------
15 private void asynchttpPost() { 16
17 String url = "http://apis.juhe.cn/mobile/get?"; 18 AsyncHttpClient client = new AsyncHttpClient(); 19 RequestParams params = new RequestParams(); 20 params.put("phone", "13666666666"); 21 params.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); 22 client.post(url, params, new AsyncHttpResponseHandler() { 23 @Override 24 public void onSuccess(String content) { 25 // TODO Auto-generated method stub
26 super.onSuccess(content); 27 Toast.makeText(MainActivity.this, content, 1).show(); 28 } 29
30 @Override 31 public void onFailure(Throwable error) { 32 // TODO Auto-generated method stub
33 super.onFailure(error); 34 Toast.makeText(MainActivity.this, "请求失败", 1).show(); 35 } 36
37 }); 38
39 } 40
41 // --------------------Get()方式请求网络---------------------------------------------
42 private void asynchttpGet() { 43 AsyncHttpClient client = new AsyncHttpClient(); 44 String url = "http://apis.juhe.cn/mobile/get?phone=13666666666&key=335adcc4e891ba4e4be6d7534fd54c5d"; 45 client.get(url, new AsyncHttpResponseHandler() { 46
47 @Override 48 public void onSuccess(String content) { 49 // TODO Auto-generated method stub
50 super.onSuccess(content); 51 Toast.makeText(MainActivity.this, content, 1).show(); 52 } 53
54 @Override 55 public void onFailure(Throwable error) { 56 // TODO Auto-generated method stub
57 super.onFailure(error); 58 Toast.makeText(MainActivity.this, "请求失败", 1).show(); 59 } 60 }); 61
62 } 63
64 }
2.Android-async-http回调逻辑的二次封装
1 public class RequestUtils { 2
3 public static AsyncHttpClient client=new AsyncHttpClient(); 4 public static void ClientGet(String url, NetCallBack cb) { 5 client.get( url, cb); 6 } 7
8
9 public static void ClientPost(String url, RequestParams params,NetCallBack cb) { 10 client.post(url,params, cb); 11 } 12 }
1 public abstract class NetCallBack extends AsyncHttpResponseHandler { 2 @Override 3 public void onStart() { 4 // TODO Auto-generated method stub
5 super.onStart(); 6
7 System.out.println("--->>请求开始,弹出进度条"); 8 } 9
10 @Override 11 public void onSuccess(String content) { 12 // TODO Auto-generated method stub
13 super.onSuccess(content); 14 onMySuccess(content); 15 System.out.println("--->>请求Success,隐藏进度条" + content); 16 } 17
18 @Override 19 public void onFailure(Throwable error) { 20 // TODO Auto-generated method stub
21 super.onFailure(error); 22 onMyFailure(error); 23 System.out.println("--->>Failure"); 24 } 25
26 public abstract void onMySuccess(String result); 27
28 public 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 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 //asynchttpGet();
14 asynchttpPost(); 15 } 16 // --------------------Post()方式请求网络---------------------------------------------
17 private void asynchttpPost() { 18
19 String url = "http://apis.juhe.cn/mobile/get?"; 20
21 RequestParams params=new RequestParams(); 22 params.put("phone", "13666666666"); 23 params.put("key", "335adcc4e891ba4e4be6d7534fd54c5d"); 24 RequestUtils.ClientPost(url, params,new NetCallBack() { 25
26 @Override 27 public void onMySuccess(String result) { 28 // TODO Auto-generated method stub
29 Toast.makeText(MainActivity.this, result, 1).show(); 30 } 31
32 @Override 33 public void onMyFailure(Throwable error) { 34 // TODO Auto-generated method stub
35 Toast.makeText(MainActivity.this,"请求失败", 1).show(); 36 } 37 }); 38
39
40
41 }