Android使用 asynchttpclient发送数据

首先这是一个第三方开源的包,要先下载包

compile 'com.loopj.android:android-async-http:1.4.9'

其次,这是需要联网的,也不要忘记添加权限

<uses-permission android:name="android.permission.INTERNET" />

发送数据的方式有两种get和post,下面来看一下代码吧
首先来看一下get:

public class Httpurl {
    private static final String BASE="http://192.168.1.105:8890/type/jason/action/";
    private static AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
    public static void get(String url, RequestParams requestParams, AsyncHttpResponseHandler asyncHttpResponseHandler){
        asyncHttpClient.get(getAbsoluteUrl(url),requestParams,asyncHttpResponseHandler);
    }


    private static  String getAbsoluteUrl(String url){
        return BASE+url;
    }
}
 final RequestParams requestParams=new RequestParams();
        requestParams.put("username","airHumidity");//传入值
    Httpurl.get("getSensor",requestParams, new TextHttpResponseHandler() {
    @Override   //回调方法,
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
        Toast.makeText(Main2Activity.this, "shibai", Toast.LENGTH_SHORT).show();
    }

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

再来看一下post,用来实现json数据发送

 AsyncHttpClient client=new AsyncHttpClient();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("Blower",0);
            jsonObject.put("WaterPump",0);
            jsonObject.put("Roadlamp",0);
            jsonObject.put("Buzzer",0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ByteArrayEntity entity = null;
        //105
        String url="http://192.168.1.101:8890/type/jason/action/control/";
        try {
           entity=new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        http://192.168.1.105:8890/type/jason/action/control/{'Buzzer':1}
       client.post(Main2Activity.this,url,entity,"application/json",new JsonHttpResponseHandler(){
           @Override
           public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
               super.onSuccess(statusCode, headers, response);
               Log.e("s", "onSuccess: "+response.toString() );
               Toast.makeText(Main2Activity.this, response.toString(), Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
               super.onFailure(statusCode, headers, responseString, throwable);
               Log.e("s", "onSuccess: "+responseString.toString() );
               Toast.makeText(Main2Activity.this, responseString.toString(), Toast.LENGTH_SHORT).show();
           }
       });

你可能感兴趣的:(Android使用 asynchttpclient发送数据)