Intelligent Agriculture: AsyncHttpClient acquiring sensors and controlling sand table

Benefits of using AsyncHttpClient

It can greatly simplify the operation. All requests are independent of the UI main thread, and the request results are processed by callback method.

Basic steps of Android async http

1. Create an AsyncHttpClient

2. Set request parameters through RequestParams object

3. Call a get method of AsyncHttpClient, pass the callback interface implementation you need (in case of success or failure), which is generally anonymous inner class, and implement asynchttppresponsehandler. The class library also provides a number of ready-made response handler s

4. Use the JsonHttpResponseHandler class to automatically parse the response results into json format
5. Use RequestParams class to create GET/POST parameters

Get sensor

First you need to import a packet

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

First, create a new class of httpuril, and then communicate.

 private  static  final String BASE_URL="http://192.168.1.101:8890/type/jason/action/";
    private static AsyncHttpClient client=new AsyncHttpClient();


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

    private static String getAbsoluteUrl(String Url){
        return BASE_URL+Url;
    }
This is the code of MainActivity. Note that TextHttpResponseHandler is used here. If it succeeds later, it will return responseString instead of a byte array.
        RequestParams requestParams=new RequestParams();
        requestParams.put("admin","airHumidity");

        HtttpUril.get("getSensor", requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Toast.makeText(MainActivity.this, "Failed to get network permission", Toast.LENGTH_SHORT).show();
                Log.e("MainActivity","222222222222222222222222222");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();
                Log.e("MainActivity","11111111111111111111111111111111");
            }
        });
   Finally, success will return a string of JSON interface. If it fails, it will return failed.  
{"result":"ok","airHumidity":48,"airTemperature":25,"soilTemperature":25,"co2":373,"soilHumidity":47,"light":6666}

{ 'result':'failed'}

Control the sand table and communicate directly

Because we have control here, so the address is like this. " http://192.168.1.101:8890/type/jason/action/control ". We need to ask parameters not to be confused with return parameters. Run it and you can communicate on the sandbox.

        AsyncHttpClient client = new AsyncHttpClient();
        final String url = "http://192.168.1.101:8890/type/jason/action/control";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("Buzzer","1");
            jsonObject.put("Roadlamp","1");
            jsonObject.put("Blower","1");
            jsonObject.put("WaterPump","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();
        }

        client.post(MainActivity.this,url,entity,"application/json",new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("rs",response.toString());
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
            }
        });

Address of the packet

Search on GitHub website https://github.com/loopj/android-async-http.

Tags: Android JSON github network

Posted on Thu, 02 Apr 2020 22:00:10 -0700 by dshevnock