Android to encapsulate its access
Callable are similar to runnable interfaces, classes that implement callable interfaces, and classes that implement runnable, are tasks that can be performed by other threads.
There are several differences between callable and runnable:
(1) The method prescribed by callable is call (), and the method specified by runnable is run ().
(2) callable can return a value after a task is executed, and Runnable's task cannot return a value.
(3) The call () method throws an exception, and the run () method cannot throw an exception.
(4) Running callable task can get a future object,
The future represents the result of an asynchronous calculation. It provides a way to check whether the calculation is complete, to wait for the completion of the calculation, and to retrieve the results of the calculation.
The future object allows you to understand task execution, cancel the execution of a task, and get the results of a task execution.
Clients get server-side data through JSON
/** * */ PackageOrg.crazyit.auction.client.util;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Map;Importjava.util.concurrent.Callable;ImportJava.util.concurrent.FutureTask;ImportOrg.apache.http.HttpResponse;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.HttpClient;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.impl.client.DefaultHttpClient;ImportOrg.apache.http.message.BasicNameValuePair;Importorg.apache.http.util.EntityUtils;/*** Description: * <br/> website: <a href= "http://www.crazyit.org"> Crazy Ava League </a> * <br/>copyright (C), 2001-2014, Yeeku.h.lee * <br/>this program was protected by CO Pyright laws. * <br/>program Name: * <br/>date: *@authorYeeku.h.lee [email protected] *@version1.0*/ Public classhttputil{//Create a HttpClient object Public StaticHttpClient HttpClient =Newdefaulthttpclient (); Public Static FinalString base_url = "Http://10.10.9.51:8080/auction/android/"; /** * * @paramURL to send the request to *@returnServer Response String *@throwsException*/ Public StaticString Getrequest (FinalString URL)throwsException {futuretask<String> task =NewFuturetask<string>( NewCallable<string>() {@Override PublicString Call ()throwsException {//creates a HttpGet object. HttpGet get =Newhttpget (URL); //send a GET requestHttpResponse HttpResponse =Httpclient.execute (GET); //If the server succeeds in returning the response if(Httpresponse.getstatusline (). Getstatuscode ()= = 200) { //Get Server response stringString result =entityutils. ToString (Httpresponse.getentity ()); returnresult; } return NULL; } }); NewThread (Task). Start (); returnTask.get (); } /** * @paramURL to send the request to *@paramparams request parameter *@returnServer Response String *@throwsException*/ Public StaticString Postrequest (FinalString URL,FinalMap<string,string> Rawparams)throwsException {futuretask<String> task =NewFuturetask<string>( NewCallable<string>() {@Override PublicString Call ()throwsException {//creates a HttpPost object. HttpPost post =Newhttppost (URL); //The passed parameters can be encapsulated if the number of passed arguments is more than onelist<namevaluepair> params =NewArraylist<namevaluepair>(); for(String key:rawParams.keySet ()) {//Encapsulate Request ParametersParams.add (NewBasicnamevaluepair (Key, Rawparams.get (key)); } //Set Request ParametersPost.setentity (Newurlencodedformentity (params,"GBK")); //send a POST requestHttpResponse HttpResponse =Httpclient.execute (POST); //If the server succeeds in returning the response if(Httpresponse.getstatusline (). Getstatuscode ()= = 200) { //Get Server response stringString result =entityutils. ToString (Httpresponse.getentity ()); returnresult; } return NULL; } }); NewThread (Task). Start (); returnTask.get (); }}
Service side
Public classBaseservletextendshttpservlet{PrivateApplicationContext CTX; Public voidinit (servletconfig config)throwsservletexception {Super. init (config); //get ApplicationContext instances in a web AppCTX =webapplicationcontextutils. Getwebapplicationcontext (Getservletcontext ()); } //return to the spring container in the web App PublicApplicationContext Getctx () {return This. CTX; }}
@WebServlet (urlpatterns= "/android/login.jsp") Public classLoginservletextendsbaseservlet{ Public voidService (HttpServletRequest request, httpservletresponse response)throwsIOException, servletexception {String user= Request.getparameter ("User"); String Pass= Request.getparameter ("Pass"); //get the business logic component of the systemAuctionmanager Auctionmanager = (Auctionmanager) getctx (). Getbean ("Mgr"); //Verifying user logon intUserId =auctionmanager.validlogin (user, pass); Response.setcontenttype ("Text/html; CHARSET=GBK "); //Login Successful if(UserId > 0) {request.getsession (true). SetAttribute ("UserId", userId); } Try { //encapsulating the validated userid into a jsonobjectJsonobject jsonobj =NewJsonobject (). Put ("UserId", userId); //Output ResponseResponse.getwriter (). println (Jsonobj.tostring ()); } Catch(Jsonexception ex) {ex.printstacktrace (); } }}
Android HttpServlet Json Data interaction