Android Learning xutils---httputils module

Source: Internet
Author: User

Httputils is an essential tool class for solving the tedious uploading and downloading of files and various get and post requests during the daily work.

Through this class, it is very convenient to do the network access download and so on, no longer need to write the lengthy code.

HttpGet requests and HttpPost requests

 

  

HTTP defines different ways to interact with the server, with 4 basic methods, namely get,post,put,delete. one of the most common is that get,post is used for information acquisition according to the HTTP specification, and should be secure and idempotent. according to the HTTP specification, post represents a request that might modify a resource on a variable server. The data of the GET request is appended to the URL (that is, the data is placed in the HTTP protocol header), to split the URL and transfer the data, the parameters are connected to &, such as: login.action ? name=hyddd&password=idontknow&verify=%e4
%BD%A0%E5%A5%BD. If the data is an English letter/number, send it as is, if it is a space, converted to +, if it is Chinese/other characters, then directly to the string with BASE64 encryption, to derive such as:%E4%BD%A0%E5%A5%BD, where%xx
XX is the ASCII represented by the symbol in 16 binary. Because the parameters that are submitted by the Get method are visible, it is possible to cause the data to leak, in this respect, Get no post security. The data submitted by the Get method can only be 1024 bytes, in theory there is no limit to post, and a large number of data post will be sent to the packet of the HTTP packet. Relatively safe, capable of transmitting more data about get and POST request mode For more information, see: http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html

How do httputils make requests in two ways?

The Httputils class has a Send method, as follows:

Send (Httprequest.httpmethod method, String URL, requestparams params,



Send (Httprequest.httpmethod method, String url,requestcallback<t> callBack)



Send method parameter list:
1. Httprequest.httpmethod method Request method
HttpRequest.HttpMethod.GET GET Mode
HttpRequest.HttpMethod.POST POST Mode
2.String URL Request URLs
3.RequestParams params Parameter object, objects stored as key-value pairs, automatically spliced (without parameters, using the three-parameter send method)

Object creation: Requestparams params = new Requestparams ();
Params.addquerystringparameter ("Method", "info");
Params.addquerystringparameter ("Access_token",
"3.1042851f652496c9362b1cd77d4f849b.2592000.1377530363.3590808424-248414");
4.requestcallback<t> callBack Callback Notification interface,
Onsuccess callback method after a successful request for this action
OnFailure failed callback, not referring to the server return data tells you to fail, but there is a network error ah, the server problem ah, and so on this error

httputils httputils=Newhttputils (); Httputils.send (HttpRequest.HttpMethod.GET,"Http://www.baidu.com",NewRequestcallback<string>() {@Override//callback after the request action succeeds             Public voidOnsuccess (responseinfo<string>responseinfo) {

} //Request action failed callback, not referring to the server return data tells you to fail, but a network error ah, ah, the server problems and so on.@Override Public voidonfailure (httpexception error, String msg) {
} });

Uploading a file also uses the Send method.

Download file:

httputils Download method (supports breakpoint continuation): The following are the overloads of the method: parameters: 1. String URL 2. String Target Download file storage location 3. Requestcallback<file> callback Callback notification interface download (string url, string Target,requestcallback<File>callback) {} download (string URL, string target,BooleanAutoresume, requestcallback<file>callback) {}          4. When Boolean Autoresume is true, if the destination file exists, then the unfinished portion continues to download. 5.boolean When Autorename is true, if the file name is obtained from the request return information, the download is automatically renamed after the download (string URL, string target,BooleanAutoresume,BooleanAutorename, requestcallback<file>callback) {} //plus parameter download download (string URL, string target, requestparams params, requestcallback<File>callback) {} download (string URL, string target, requestparams params,BooleanAutoresume, requestcallback<file>callback) {} download (string URL, string target,requestparams params,BooleanAutoresume,BooleanAutorename, requestcallback<file>callback) {} //Specify access network mode Download (Httprequest.httpmethod method, string url, string target,requestparams pa Rams, Requestcallback<File>callback) {} download (Httprequest.httpmethod method, string url, string target,requestparams params,BooleanAutoresume, requestcallback<file>callback) {} download (Httprequest.httpmethod method, string URL, string target, requestparams params,BooleanAutoresume,BooleanAutorename, requestcallback<file>callback) {}        

Here are some of my own summaries, more and more detailed knowledge we look here: http://my.oschina.net/jack1900/blog/174439

The talk is very fine, also very easy to understand, everybody studies together.

Android Learning xutils---httputils module

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.