Android's encapsulated asynchronous network request framework

1. Introduction
Network requests in Android generally use Apache HTTP Client or HttpURLConnection, but using these two libraries directly requires writing a lot of code to complete network post and get requests, and using this MyHttpUtils library can greatly simplify the operation, it is Based on HttpURLConnection, all requests are independent from the main UI thread, and the request results are not processed through the CommCallback callback method. Without sub-threads and handles, the chain becomes clearer .

2. Features

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

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. The json data returned during use can be automatically parsed and returned 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)//The requested url
                .setJavaBean(UserBean.class)//Set the javabean object that needs to be resolved into
                .setReadTimeout(60000)//Set the read timeout time, if not set, the default is 30s (30000)
                .setConnTimeout(6000)//Set the connection timeout time, if not set, the default is 5s (5000)
                .onExecute(new CommCallback<UserBean>() {//Start executing the asynchronous request, pass in a generic callback object, the generic type is 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 when failed
                        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>() {///The entity class is automatically resolved
                    @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);
                    }
                });
    }


document dowload:

 

 

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 where the file is saved, excluding the file name
                .setReadTimeout(5 * 60 * 1000)//Because it takes a lot of time to download files, set the read time to 5 minutes
                .downloadFile(new CommCallback<String>() {
                    @Override
                    public void onSucess(String msg) {
                        Util.showMsg(MainActivity.this, msg);
                    }

                    @Override
                    public void onFailed(String s) {

                    }
                    /**
                     * Can override progress callback method
                     * @param total
                     * @param current
                     */
                    @Override
                    public void onDownloading(long total, long current) {
                        tvProgress.setText("当前进度:" + new DecimalFormat("######0.00").format(((double) current / total) * 100) + "%");
                    }
                });
    }


Don't forget to add network permissions

 

 

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


File upload and download also need to add permissions

 

 

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

 

 

Source code click to download

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326271537&siteId=291194637