Today introduced xutils the last module--httputils, dragged so long, finally to end. In addition, the code word is not easy, if you have any questions and opinions, welcome message to discuss. Httputils is the necessary tool to solve the complicated process of uploading and downloading files as well as various get and post requests, through which the open source is very easy to focus on the business of the interface, no longer need to write lengthy code. The following full text revolves around four center points, namely: HttpGet request, HttpPost request, download file and upload file.
httpget request . believe that the development of Android Network application of the students will not be unfamiliar with this, long-term development will allow you to accumulate the next reusable tool class. And Xutils is helping us to integrate those tool classes into a more extensible helper class. For example, Htttpget request, here is only a brief introduction of the key application method, the specific details also please everyone to download xutils Source to study the research, we introduced enough to apply to our application development. The usual HttpGet request will put a series of request parameters on the back of the request address, drag out a long tail, very annoying, here can be like the parameters of the HttpPost request to form a similar Basicnamevaluepair object directly encapsulated in the request method, Eliminate the hassle of stitching URLs, and you can set the time-out. In addition, a callback class is provided in the requested method, which has a callback method that handles different request results, such as a callback in the request process, a callback for a successful request, and a callback when the request is in error. Below the code to see directly.
Requestparams params =NewRequestparams (); Params.addquerystringparameter ("Method", "info"); Params.addquerystringparameter ("Access_token", "3.1042851f652496c9362b1cd77d4f849b.2592000.1377530363.3590808424-248414"); Httputils http=Newhttputils (); Http.configcurrenthttpgetcacheexpiry (1000 * 10); Http.send (HttpRequest.HttpMethod.GET,"Https://pcs.baidu.com/rest/2.0/pcs/quota", params,NewRequestcallback<string>() {@Override Public voidOnStart () {Resulttext.settext ("Conn ..."); } @Override Public voidOnloading (LongTotalLongCurrent ) {Resulttext.settext ( current+ "/" +Total ); } @Override Public voidonsuccess (String result) {Resulttext.settext ("Response:" +result); } @Override Public voidonfailure (httpexception error, String msg) {resulttext.settext (msg); } });
HttpPost request . In order to unify the style of the request, the way of HttpPost request and httpget almost can be said to be exactly the same, the same provides a variety of different results of the callback method, we see the following code to know, is really at a glance.
Requestparams params =NewRequestparams (); Params.addquerystringparameter ("Method", "mkdir"); Params.addquerystringparameter ("Access_token", "3.1042851f652496c9362b1cd77d4f849b.2592000.1377530363.3590808424-248414"); Params.addbodyparameter ("Path", "/apps/Test app/test folder"); Httputils http=Newhttputils (); Http.send (HttpRequest.HttpMethod.POST,"Https://pcs.baidu.com/rest/2.0/pcs/file", params,NewRequestcallback<string>() {@Override Public voidOnStart () {Resulttext.settext ("Conn ..."); } @Override Public voidOnloading (LongTotalLongCurrent ) {Resulttext.settext ( current+ "/" +Total ); } @Override Public voidonsuccess (String result) {Resulttext.settext ("Upload response:" +result); } @Override Public voidonfailure (httpexception error, String msg) {resulttext.settext (msg); } });
Here is a very useful feature, that is, through the HTTP protocol to download files , and no need to download files for Android and write a big lump of code, so long code debugging really can be angry dead. Remember once did a project, there is a need to download video, but also to support the breakpoint download, only write a download tool class to write for a day or two, plus debugging, really people are driving crazy. If Xutils had been there earlier, it would have been unnecessary to make that demand. Httputils provides developers with a very convenient download API, you can use a few simple parameters to achieve the download, or even breakpoint download function. On the code.
HttpHandler handler =Http.download (Downloadaddredit.gettext (). toString (),"/SDCARD/FILEEXPLORER.APK", true,//if the destination file exists, then the unfinished portion continues to download. true,//If the file name is obtained from the request return information, it is automatically renamed after the download is complete. NewRequestcallback<file>() {@Override Public voidOnStart () {Resulttext.settext ("Conn ..."); } @Override Public voidOnloading (LongTotalLongCurrent ) {Resulttext.settext ( current+ "/" +Total ); } @Override Public voidonsuccess (File result) {Resulttext.settext ("Downloaded:" +Result.getpath ()); } @Override Public voidonfailure (httpexception error, String msg) {Resulttext.settext (Error.getexceptioncode ()+ ":" +msg); } });
Note: If you need to pause the download during the download, you can do it with a simple line of code: Mhandler.stop (), if you set a breakpoint to download, the next time you start again, you will automatically continue downloading from the last breakpoint you downloaded.
The last feature is to upload the file , which is also very common in the project. For example, users upload avatars, such as Web applications need to upload local files to the cloud and so on. At the same time Httputils also provides developers with the upload process and upload the results of each callback interface. Everyone in the use of Httputils upload files, as long as the following code to code to quickly be able to basically meet the needs of the business.
Requestparams params =NewRequestparams (); Params.addquerystringparameter ("Method", "Upload"); Params.addquerystringparameter ("Path", "/apps/Test application/test.zip"); //Please find your access_token on the Open ACCESS_TOKENAPI test page of Baidu.Params.addquerystringparameter ("Access_token", " 3.1042851f652496c9362b1cd77d4f849b.2592000.1377530363.3590808424-248414 "); Params.addbodyparameter ("File",NewFile ("/sdcard/test.zip")); Httputils http=Newhttputils (); Http.send (HttpRequest.HttpMethod.POST,"Https://pcs.baidu.com/rest/2.0/pcs/file", params,NewRequestcallback<string>() {@Override Public voidOnStart () {Resulttext.settext ("Conn ..."); } @Override Public voidOnloading (LongTotalLongCurrent ) {Resulttext.settext ( current+ "/" +Total ); } @Override Public voidonsuccess (String result) {Resulttext.settext ("Upload response:" +result); } @Override Public voidonfailure (httpexception error, String msg) {resulttext.settext (msg); } });
Transferred from: http://my.oschina.net/jack1900/blog/174439
Android-xutils Framework Introduction (IV)