"Android" HttpClient send rest request

Source: Internet
Author: User

Direct PO Code Bar, the first is a class of enum type, is four kinds of rest HTTP request, Get/post/put/delete:

 Public enumHttprequestmethod {httpget {@Override PublicHttpurirequest createrequest (String URL) {return Newhttpget (URL);} }, HttpPost {@Override PublicHttpurirequest createrequest (String URL) {return Newhttppost (URL);} }, Httpput {@Override PublicHttpurirequest createrequest (String URL) {return Newhttpput (URL);} }, Httpdelete {@Override PublicHttpurirequest createrequest (String URL) {return Newhttpdelete (URL);}    };  PublicHttpurirequest createrequest (String URL) {return NULL; }}

Next is a Httputil class that can specify the HTTP request type, the URL to be accessed, and the parameters to be used:

 Public classHttputil {Private Static FinalString Default_local_encode = "UTF-8"; Private Static FinalString Default_remote_encode = "UTF-8"; Private Static Final inthttp_200 = 200;  Public StaticHttpClient HttpClient =Newdefaulthttpclient (); Static{httpclient.getparams (). Setparameter (Httpconnectionparams.so_timeout, integer.valueof (5000)); Httpclient.getparams (). Setparameter (Httpconnectionparams.connection_timeout, integer.valueof (3000)); }     Public Static FinalString base_url = "Localhost:8080/demo/login";  Public Staticstring SendRequest (Httprequestmethod requestmethod, string url,Finalmap<string,string> params)throwsException {FinalHttpurirequest request =requestmethod.createrequest (URL); if(Params! =NULL{(httpentityenclosingrequest) request). Setentity (Newurlencodedformentity (Maptopair (params), default_remote_encode)); } futuretask<String> task =NewFuturetask<string>(                NewCallable<string>() {@Override PublicString Call ()throwsException {httpresponse response=Httpclient.execute (Request); if(Response.getstatusline (). Getstatuscode () = =http_200) {                            returnentityutils.tostring (Response.getentity (), Default_local_encode); }                        return"Access Failed";        }                }        ); NewThread (Task). Start (); returnTask.get (); }    StaticList<namevaluepair> Maptopair (map<string, string>map) {        FinalList<namevaluepair> pairlist =NewArraylist<namevaluepair>();  for(String key:map.keySet ()) {Pairlist.add (NewBasicnamevaluepair (Key, Map.get (key)); }        returnpairlist; }}

Used in the Futuretask class related to multithreading, not very understanding of the first use, the feeling of multi-threaded, concurrent related things are quite a lot of and very important, the use of multithreading here is because there may be IO blocking, which will lead to the main interface hanging off.

The call is simple and requires only:

Httputil.sendrequest (httprequestmethod.httppost, URL, map);

Where map is a hashmap<string, string> type.

Finish ~ Although very simple, but after all, the first time to write, learn a lot, of course, also want to know how to achieve the bottom of the httpclient, must be encapsulated socket but there must be something very interesting inside.

In addition, about TCP/IP is also water depth can, next to do chat, a little bit about the long connection and short links, as well as the heartbeat mechanism and polling mechanism, will slowly write to the blog.

"Android" HttpClient send rest request

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.