AsyncHttpClient的get、post简单使用方法

1.AsyncHttpClient使用前提

首先使用AsyncHttpClient之前需要在Gradle中下载好我们所需要的包。
   compile 'com.loopj.android:android-async-http:1.4.9'

2.AsyncHttpClient使用方法

1. 单条使用
首先创建出AsyncHttpClient的对象 client ;
使用对象client点出方法  
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://www.google.com", new AsyncHttpResponseHandler()client.get("https://www.google.com", new AsyncHttpResponseHandler() {

 @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        //获取成功方法
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
        // 获取失败方法

            }


}
2.如果需要重复使用建议方法
由于需要多次使用,代码被重复利用,所以我们先创建一个公共类,每次使用时进行调用即可;
公共类.方法进行调用;
实现方法回调。
public class AsycHttp {
    private static final String HTTP="http://192.168.1.101:8890/type/jason/action/";
    private static AsyncHttpClient client=new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler){
        client.post(context,getAbsoluteUrl(url),entity,contentType,responseHandler);
    }

    private static String getAbsoluteUrl(String url) {
        return HTTP+url;
    }
}
 AsycHttp.get("getSensor", null, new TextHttpResponseHandler() {
 @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        //获取成功方法
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
        // 获取失败方法

            }

 }

3.AsyncHttpClient的get方法

既然已经知道如何使用AsyncHttpClient了,我们先来看看AsyncHttpClient中的get方法;
get方法中有三个参数:url,params,responseHandler;
文档接口:http://103.244.59.105:8014/paopaoserver/categorylist?params={"classify_id":70,"page":1,"page_count":2} 
那上面的例子来说:
url就是上面的:http://103.244.59.105:8014/paopaoserver/categorylist
params为上面的:params={"classify_id":70,"page":1,"page_count":2}    
responseString 为网站中的内容。
代码如下:
public class AsycHttp {
    private static final String HTTP="http://192.168.1.101:8890/type/jason/action/";
    private static AsyncHttpClient client=new AsyncHttpClient();/
//get方法;
    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler){
        client.post(context,getAbsoluteUrl(url),entity,contentType,responseHandler);
    }

    private static String getAbsoluteUrl(String url) {
        return HTTP+url;
    }
}
     RequestParams requestParams = new RequestParams();
     requestParams.put("params", "{\"classify_id\":70,\"page\":1,\"page_count\":2}");

                AsycHttp.get("getSensor", requestParams , new TextHttpResponseHandler() {
                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        Log.e("0000000", "失败");
                        Toast.makeText(Main2Activity.this, "访问失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, String responseString) {
                        Log.e("0000000", "" + responseString);
                        Toast.makeText(Main2Activity.this, responseString, Toast.LENGTH_SHORT).show();
                        }

4.AsyncHttpClient的post方法

1.AsyncHttpClient.post传递键值对类型参数:
公共类中post方法:
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.post(getAbsoluteUrl(url), params, responseHandler);
  }
使用时创建RequestParams 对象:
RequestParams params = new RequestParams();
params.put("key", "value");
params.put("more", "data")
2.AsyncHttpClient.post传递Json类型参数:
公共类中post方法:
post参数:Context ,url,HttpEntity ,contentType,responseHandler
 public static void post(Context context, String url, HttpEntity entity,String contentType, ResponseHandlerInterface responseHandler){
        client.post(context,getAbsoluteUrl(url),entity,contentType,responseHandler);
    }
首先创建JSONObject 对象 jsonObject;
将JSon数据使用jsonObject.put()传递;
比如:{'Buzzer':1}数据-> jsonObject.put("Buzzer",1);
创建ByteArrayEntity entity=null;
设置 entity;
AsycHttp.post(Context,url,entity,"application/json",new JsonHttpResponseHandler(){
public void onSuccess(...){
}
public void onFailure(...){
}
});
  JSONObject jsonObject=new JSONObject();
        try {
            if (string=="all"){
                jsonObject.put("Roadlamp",1);
                jsonObject.put("WaterPump",1);
                jsonObject.put("Blower",1);
            }else {
                jsonObject.put(string,i);
            }
            //jsonObject.put("Roadlamp",1);
            //jsonObject.put("WaterPump",1);
            //jsonObject.put("Blower",1);
            //  jsonObject.put("Buzzer",1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ByteArrayEntity entity=null;
        try {
            entity=new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        AsycHttp.post(Main2Activity.this,"control",entity,"application/json",new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("0000000",""+response);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                Log.e("0000000","失败");
            }
        });

你可能感兴趣的:(安卓)