Android's packaged asynchronous network request framework

Source: Internet
Author: User

1. Introduction
Web requests in Android typically use Apache HTTP client or httpurlconnection, but using these two class libraries directly requires a lot of code to complete the network post and get requests. The use of this myhttputils library can greatly simplify the operation, it is based on HttpURLConnection, all requests are independent of the UI thread, not through the Commcallback callback method processing request results, No child threads, Without handle, the chain turns into a clearer code.

2. Features

    1. Support GET, POST request, file download, upload, etc.;
    2. protocols that support HTTP and HTTPS;
    3. Support to set the connection, read time-out (optional);
    4. Supports JSON-formatted request results (no matter how complex the JSON format is);
    5. Support for incoming JavaBean object (parsed JavaBean object);
    6. Support the callback method in the response to the JavaBean object, so that the callback method can directly get the parsed JavaBean object;
    7. The UI is updated in support of callback methods (so it is called asynchronous request).

Description: Everything in Java is an object, where the JavaBean object is the entity that corresponds to the JSON data returned after you request the interface, and the JSON data returned when used can automatically parse and return the object based on the object you are giving.

3. Use

Gradle Add dependencies (after adding sync):

Compile ' com.huangdali:myhttputils:2.0.2 '

Get

 Public voidOnget () {String URL= "http://gpj.zhangwoo.cn/app.php?platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1&c= Member&a=getdepartments "; Newmyhttputils (). URL (URL)//the requested URL. Setjavabean (UserBean.class)//set the JavaBean object to parse into. Setreadtimeout (60000)//Set read timeout, default to 30s (30000) if not set. Setconntimeout (6000)//Set the connection time-out, default 5s if not set. OnExecute (NewCommcallback<userbean> () {//begins an asynchronous request, passing in a generic callback object, which is the returned JavaBean object@Override Public voidOnsucess (UserBean Bean) {//Callback after successUtil.showmsg (mainactivity. This, Bean.getdata (). Get (0). Getdepartname ()); } @Override Public voidOnfailed (String msg) {//Callback when failedUtil.showmsg (mainactivity. This, MSG);    }                }); }

Post:

 Public voidOnpost () {HashMap<string, string> param =NewHashmap<>(); Param.put ("C", "member"); Param.put ("A", "getdepartments"); Newmyhttputils (). URL (urls2). AddParam (param). Setjavabean (UserBean.class). Onexecutebypost (NewCommcallback<userbean> () {///entity Class Auto-parse@Override Public voidonsucess (UserBean remarkbean) {log.i ("Tag", remarkbean.tostring ()); Util.showmsg (mainactivity. This, Remarkbean.getdata (). Get (0). Getdepartname ()); } @Override Public voidonfailed (String msg) {util.showmsg (mainactivity. This, MSG);    }                }); }

File Download:

 Public voidondownload () {String URL= "Http://avatar.csdn.net/8/6/0/2_dickyqie.jpg"; Newmyhttputils (). URL (URL). Setfilesavepath ("/sdcard/downloadtest")//not here just fill in the file save path, not including the file name Oh. setreadtimeout (5 * 60 * 1000)//The set read time is 5 minutes because the download file takes a lot longer. DownloadFile (NewCommcallback<string>() {@Override Public voidonsucess (String msg) {util.showmsg (mainactivity. This, MSG); } @Override Public voidonfailed (String s) {}/*** You can override the progress callback method *@paramTotal *@param Current*/@Override Public voidOndownloading (LongTotalLongCurrent ) {Tvprogress.settext ("Current Progress:" +NewDecimalFormat ("##### #0. XX"). Format ((Double) current/total) * 100) + "%");    }                }); }

Don't forget to add network permissions

<android:name= "Android.permission.INTERNET"/>

File upload and download also need to add permissions

<android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>  <android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
SOURCE Click to download

Android's packaged asynchronous network request framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.