Android Asynchronous Http Client

 

一:先到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());
            }

        });
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值