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();
           }
       });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值