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 ''

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

 private  static  final String BASE_URL="";
    private static AsyncHttpClient client=new AsyncHttpClient();

    public  static  void get(String url, RequestParams params, AsyncHttpResponseHandler 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();

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

            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();
   Finally, success will return a string of JSON interface. If it fails, it will return failed.  

{ 'result':'failed'}

Control the sand table and communicate directly

Because we have control here, so the address is like this. " ". 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 = "";
        JSONObject jsonObject = new JSONObject();
        try {
        } catch (JSONException e) {
        ByteArrayEntity entity = null;
        try {
            entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        } catch (UnsupportedEncodingException e) {
        },url,entity,"application/json",new JsonHttpResponseHandler(){
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);

            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

Tags: Android JSON github network

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