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