[关闭]
@Yano 2016-01-19T09:02:54.000000Z 字数 3885 阅读 2869

HTTP 网络请求库框架

Android

极客学院 Android 主流开源库深度剖析:http://ke.jikexueyuan.com/xilie/108


HTTP 网络数据交互请求,是极其重要和频繁使用的模块,也是最基本的网络编程所需要学习的。学习并掌握一款好的 HTTP 请求框架,对我们的网络开发非常重要。

HTTP 请求在移动网络编程中,主要表现在 GETPOST 请求接口数据的使用。

两款优秀的开源 HTTP 网络请求框架:

  1. Volley
  2. Android-async-http

Volley

库下载地址:

https://github.com/LjyYano/Android-Open-Source-Lib-Demo/blob/master/VollyDemo/libs/Volley.jar

特点

缺点

不适合大数据、流媒体的网络请求。

使用方法

  1. public class MyApplication extends Application {
  2. public static RequestQueue queue;
  3. @Override
  4. public void onCreate() {
  5. super.onCreate();
  6. queue = Volley.newRequestQueue(getApplicationContext());
  7. }
  8. public static RequestQueue getHttpQueue() {
  9. return queue;
  10. }
  11. }
  1. public class MainActivity extends Activity {
  2. /**
  3. * 1、Volley的Get和Post请求方式的使用
  4. *
  5. * 2、Volley的网络请求队列建立和取消队列请求及Activity周期关联
  6. *
  7. */
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. vollyGet();
  13. // vollyPost();
  14. }
  15. @Override
  16. protected void onStop() {
  17. super.onStop();
  18. MyApplication.getHttpQueue().cancelAll("get");
  19. MyApplication.getHttpQueue().cancelAll("post");
  20. }
  21. private void vollyPost() {
  22. String url = "http://apis.juhe.cn/mobile/get?";
  23. StringRequest request = new StringRequest(Method.POST, url,
  24. new Listener<String>() {
  25. public void onResponse(String arg0) {
  26. Toast.makeText(MainActivity.this, arg0,
  27. Toast.LENGTH_LONG).show();
  28. }
  29. }, new Response.ErrorListener() {
  30. public void onErrorResponse(VolleyError arg0) {
  31. Toast.makeText(MainActivity.this, "网络请求失败",
  32. Toast.LENGTH_LONG).show();
  33. }
  34. }) {
  35. @Override
  36. protected Map<String, String> getParams() throws AuthFailureError {
  37. HashMap<String, String> map = new HashMap<String, String>();
  38. map.put("phone", "13666666666");
  39. map.put("key", "335adcc4e891ba4e4be6d7534fd54c5d");
  40. return map;
  41. }
  42. };
  43. // Tag用于寻找
  44. request.setTag("post");
  45. MyApplication.getHttpQueue().add(request);
  46. }
  47. private void vollyGet() {
  48. String url = "http://apis.juhe.cn/mobile/get?phone=15822882820&key=335adcc4e891ba4e4be6d7534fd54c5d";
  49. StringRequest request = new StringRequest(Method.GET, url,
  50. new Listener<String>() {
  51. public void onResponse(String arg0) {
  52. Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG)
  53. .show();
  54. }
  55. }, new Response.ErrorListener() {
  56. public void onErrorResponse(VolleyError arg0) {
  57. Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_LONG)
  58. .show();
  59. }
  60. });
  61. // Tag用于寻找
  62. request.setTag("get");
  63. MyApplication.getHttpQueue().add(request);
  64. }
  65. }

注意

AndroidManifest 文件中,加入 MyApplication 和网络权限。

Async-http

库下载地址

https://github.com/LjyYano/Android-Open-Source-Lib-Demo/blob/master/AsyncHttpDemo/libs/android-async-http-1.4.3.jar

特点

使用方法

  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. // asynchttpGet();
  7. asynchttpPost();
  8. }
  9. private void asynchttpPost() {
  10. AsyncHttpClient client = new AsyncHttpClient();
  11. String url = "http://apis.juhe.cn/mobile/get?";
  12. RequestParams params = new RequestParams();
  13. params.put("phone", "13666666666");
  14. params.put("key", "335adcc4e891ba4e4be6d7534fd54c5d");
  15. client.post(url, params, new AsyncHttpResponseHandler() {
  16. @Override
  17. public void onSuccess(String arg0) {
  18. super.onSuccess(arg0);
  19. Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG)
  20. .show();
  21. }
  22. @Override
  23. public void onFailure(Throwable arg0) {
  24. super.onFailure(arg0);
  25. Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_LONG)
  26. .show();
  27. }
  28. });
  29. }
  30. private void asynchttpGet() {
  31. AsyncHttpClient client = new AsyncHttpClient();
  32. String url = "http://apis.juhe.cn/mobile/get?phone=13666666666&key=335adcc4e891ba4e4be6d7534fd54c5d";
  33. client.get(url, new AsyncHttpResponseHandler() {
  34. @Override
  35. public void onSuccess(String arg0) {
  36. super.onSuccess(arg0);
  37. Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG)
  38. .show();
  39. }
  40. @Override
  41. public void onFailure(Throwable arg0) {
  42. Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_LONG)
  43. .show();
  44. super.onFailure(arg0);
  45. }
  46. });
  47. }
  48. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注