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
- Support GET, POST request, file download, upload, etc.;
- protocols that support HTTP and HTTPS;
- Support to set the connection, read time-out (optional);
- Supports JSON-formatted request results (no matter how complex the JSON format is);
- Support for incoming JavaBean object (parsed JavaBean object);
- Support the callback method in the response to the JavaBean object, so that the callback method can directly get the parsed JavaBean object;
- 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