Android Asynchronous Http Client
来源:互联网 发布:斯凯奇淘宝旗舰店真假 编辑:程序博客网 时间:2024/06/05 11:14
一:先到http://loopj.com/android-async-http下载jar包,并导入到工程中
二:先简单封装一个网络请求引擎(仅仅处理了无参、有参的GEP、POST常用请求,其他功能参看http://loopj.com/android-async-http自行封装)
package com.analysys.asynchttpclientdemotwo.HttpRequestEngine;import com.loopj.android.http.*;/** * Created by MQL on 2016/9/27. * 功能:提供网络请求 */public class HttpRequestEngine { private static AsyncHttpClient client = new AsyncHttpClient(); //不带参数的get请求 public static void get(String url, AsyncHttpResponseHandler responseHandler) { client.get(url, responseHandler); } //不带参数的post请求 public static void post(String url, AsyncHttpResponseHandler responseHandler) { client.post(url, responseHandler); } //带参数的get请求 public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { client.get(url, params, responseHandler); } //带参数的post请求 public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { client.post(url, params, responseHandler); }}
三:接下来,直接使用网络引擎中的类方法请求网络
private void get(){ HttpRequestEngine.get("http://api2.xxx.cn:8089", new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { // If the response is JSONObject instead of expected JSONArray try { int code = response.getInt("code"); Log.d("MQL", "onSuccess with code = " + code); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { Log.d("MQL", throwable.toString()); } }); } private void post(){ HttpRequestEngine.post("http://api2.xxx.cn:8089", new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { // If the response is JSONObject instead of expected JSONArray try { int code = response.getInt("code"); Log.d("MQL", "onSuccess with code = " + code); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { Log.d("MQL", throwable.toString()); } }); } private void getWithParam() { RequestParams params = new RequestParams(); params.put("app3", "123"); HttpRequestEngine.get("http://api2.xxx.cn:8089", params, new JsonHttpResponseHandler(){ @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { // If the response is JSONObject instead of expected JSONArray try { int code = response.getInt("code"); Log.d("MQL", "onSuccess with code = " + code + ", url = " + this.getRequestURI().toString()); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { Log.d("MQL", throwable.toString()); } }); } private void postWithParam() { RequestParams params = new RequestParams(); params.put("app3", "123"); HttpRequestEngine.post("http://api2.xxx.cn:8089", params, new JsonHttpResponseHandler(){ @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { // If the response is JSONObject instead of expected JSONArray try { int code = response.getInt("code"); Log.d("MQL", "onSuccess with code = " + code + ", url = " + this.getRequestURI().toString()); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { Log.d("MQL", throwable.toString()); } }); }}
0 0
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- JavaWEB查询数据库信息(JSTL)
- matlab SVM有监督学习
- ssh反连
- Linux系统信息
- Matlab中画图的说明
- Android Asynchronous Http Client
- 华为oj之求int型正整数在内存中存储时1的个数
- 添加环境变量
- Bluetooth的接收数据测试代码
- 使用windbg查看PE结构
- Java8新特性浅析
- 错误 Failed resolution of: Landroid/support/v7/internal/widget/TintManager;解决办法
- 人脸识别必读的N篇文章
- c++中变量和传参时候的拷贝的理解