An Encapsulated Asynchronous Network Request Framework for Android

Keywords: Android JSON network Apache

1. introduction
In Android, network requests usually use Apache HTTP Client or HttpURLConnection, but directly using these two libraries requires a lot of code to complete network post s and get requests. Using this MyHttpUtils library can greatly simplify the operation. It is based on HttpURLConnection, and all requests are independent of the UI main thread, without calling back through CommCallback. The method handles the result of the request, without sub-threads and handle, and the chain-like transformation makes the code clearer. .

2. characteristics

  1. Support get, post request, file download, upload, etc.
  2. Supporting http and https protocols;
  3. Support setting connection and reading timeout time (optional);
  4. Support json format request results (no matter how complex the json format is, it can be done);
  5. Support for incoming JavaBean objects (parsed JavaBean objects);
  6. Support callback method to react to the incoming javabean object, so that the parsed javabean object can be obtained directly in the callback method.
  7. Supports updating UI in callback methods (so called asynchronous requests).

Description: Everything in java is an object. The JavaBean object here is the entity corresponding to the json data returned after you request the interface. When you use the json data returned, you can automatically parse and return the object according to the object you give.

3. use

gradle adds dependencies (Sync after adding):

compile 'com.huangdali:myhttputils:2.0.2'


get:

 public void onGet() {
        String url = "http://gpj.zhangwoo.cn/app.php?platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1&c=member&a=getdepartments";
        new MyHttpUtils()
                .url(url)//Requested url
                .setJavaBean(UserBean.class)//Setting up javabean objects that need to be parsed
                .setReadTimeout(60000)//Set the read timeout time, if not set, the default is 30s(30000)
                .setConnTimeout(6000)//Set the connection timeout, if not, default 5s(5000)
                .onExecute(new CommCallback<UserBean>() {//Start executing the asynchronous request, pass in a generic callback object, generic for the returned javabean object
                    @Override
                    public void onSucess(UserBean bean) {//Callback after success
                        Util.showMsg(MainActivity.this, bean.getData().get(0).getDepartname());
                    }

                    @Override
                    public void onFailed(String msg) {//Callback in case of failure
                        Util.showMsg(MainActivity.this, msg);
                    }
                });
    }

post:

public void onPost() {
        HashMap<String, String> param = new HashMap<>();
        param.put("c", "member");
        param.put("a", "getdepartments");
        new MyHttpUtils()
                .url(urls2)
                .addParam(param)
                .setJavaBean(UserBean.class)
                .onExecuteByPost(new CommCallback<UserBean>() {/// Automatic Resolution of Entity Classes
                    @Override
                    public void onSucess(UserBean remarkBean) {
                        Log.i("tag",remarkBean.toString());
                        Util.showMsg(MainActivity.this, remarkBean.getData().get(0).getDepartname());
                    }
                    @Override
                    public void onFailed(String msg) {
                        Util.showMsg(MainActivity.this, msg);
                    }
                });
    }

File download:

public void onDownload() {
        String url = "http://avatar.csdn.net/8/6/0/2_dickyqie.jpg";
        new MyHttpUtils()
                .url(url)
                .setFileSavePath("/sdcard/downloadtest")//Don't just fill in the path to save the document, not including the name of the document.
                .setReadTimeout(5 * 60 * 1000)//Because downloading files takes a lot of time, the read time is set to 5 minutes.
                .downloadFile(new CommCallback<String>() {
                    @Override
                    public void onSucess(String msg) {
                        Util.showMsg(MainActivity.this, msg);
                    }

                    @Override
                    public void onFailed(String s) {

                    }
                    /**
                     * Schedule callback method can be rewritten
                     * @param total
                     * @param current
                     */
                    @Override
                    public void onDownloading(long total, long current) {
                        tvProgress.setText("Current progress:" + new DecimalFormat("######0.00").format(((double) current / total) * 100) + "%");
                    }
                });
    }

Don't forget to add network privileges

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

File upload and download also require additional permissions

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

Source Click Download

Posted by jomofee on Fri, 22 Mar 2019 04:36:53 -0700