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
原创粉丝点击