First, on the server side, the star uses the SSH framework. struts2 integrates the JSON plug-in, and the server and client information exchange is transmitted in JSON format. Because struts2 is used on the server side, therefore, a JSON plug-in is installed on the star, so that the server information is easily sent to the mobile phone client in the form of JSON ~~ The following is the code. You are welcome to shoot bricks from EOE ~~
First, build an SSH framework on the server side. The details are not stated ~ The struts xml configuration is as follows:
Java code:
- <Package name = "login" extends = "JSON-Default">
- <Action name = "login" class = "com. jclick. Test. loginaction" method = "login">
- <Result type = "JSON"> <paramname = "includeproperties"> result </param> </result>
- </Action>
- </Package>
Copy code
The Code on the mobile phone is as follows:
First, there is a caching class on the mobile phone end, which is mainly used to cache data that needs to be accessed on the mobile phone end. The advantage of this class is that datav can save the interaction between the mobile phone and the server, which is implemented in a single example:
Java code:
- Package com. jclick. cache;
- Import com. jclick. Bean. user;
- Public class cache {
- Private user;
- Private cache (){
- }
- /** Create a singleton */
- Private Static class cacheholder {
- Private Static final cache instance = new cache ();
- }
- Public cache getinstance (){
- Return cacheholder. instance;
- }
- Public user getuser (){
- Return user;
- }
- Public void setuser (User user ){
- This. User = user;
- }
- }
Copy code
Then, the user starts to write the protocol on the mobile phone end, sends a request to the server, and the server sends feedback to the mobile phone end:
Java code:
- Package com. jclick. Protocol;
- Import java. Io. bufferedreader;
- Import java. Io. inputstreamreader;
- Import java. util. arraylist;
- Import java. util. List;
- Import org. Apache. http. httpresponse;
- Import org. Apache. http. namevaluepair;
- Import org. Apache. http. Client. httpclient;
- Import org. Apache. http. Client. entity. urlencodedformentity;
- Import org. Apache. http. Client. Methods. httppost;
- Import org. Apache. http. impl. Client. defaulthttpclient;
- Import org. Apache. http. Message. basicnamevaluepair;
- Import org. JSON. jsonexception;
- Import org. JSON. jsonobject;
- Public class baseprotocol {
- Private stringbuilder sb = new stringbuilder ();
- Private httpclient;
- Private httppost httprequest;
- Private httpresponse response;
- Private list <namevaluepair> namevaluepair = new arraylist <namevaluepair> ();
- Baseprotocol (){
- Httpclient = new defaulthttpclient ();
- }
- /**
- * Send a request to the server
- *
- * @ Param URL
- * @ Throws exception
- */
- Protected void pack (string URL) throws exception {
- Httpclient = new defaulthttpclient ();
- Httprequest = new httppost (URL );
- Httprequest. setentity (New urlencodedformentity (namevaluepair ));
- Response = httpclient.exe cute (httprequest );
- }
- /**
- * Get the returned data
- *
- * @ Param URL
- * @ Return
- * @ Throws exception
- */
- Protected void parse () throws exception {
- // Todo status processing 500 200
- If (response. getstatusline (). getstatuscode () = 200 ){
- Bufferedreader bufferedreader2 = new bufferedreader (
- New inputstreamreader (response. getentity (). getcontent ()));
- For (string S = bufferedreader2.readline (); s! = NULL; S = bufferedreader2
- . Readline ()){
- SB. append (s );
- }
- }
- }
- /**
- * Send information to the server
- *
- * @ Param key
- * @ Param Value
- */
- Public void addnamevaluepair (string key, string value ){
- Namevaluepair. Add (New basicnamevaluepair (Key, value ));
- }
- /**
- * Returns the jsonobject data model.
- *
- * @ Return
- * @ Throws jsonexception
- */
- Public jsonobject getjson () throws jsonexception {
- Return new jsonobject (sb. tostring ());
- }
- }
Copy code
Next is the login protocol. Here, the stars are just a class used to simulate login, for your reference only:
Java code:
- Package com. jclick. Protocol;
- Import org. JSON. jsonobject;
- Import com. jclick. Bean. user;
- Public class loginprotocol extends baseprotocol {
- Private Final Static string url = "http://localhost: 8080/test/login ";
- Public Boolean checklogin (User USR ){
- Try {
- Pack (URL );
- Parse ();
- Jsonobject OBJ = This. getjson ();
- If (obj. getstring ("result"). Equals ("failed ")){
- Return false;
- } Else {
- Return true;
- }
- } Catch (exception e ){
- E. printstacktrace ();
- Return false;
- }
- }
- }
Copy code
Then the user entity class is used to save user information:
Java code:
- Package com. jclick. Bean;
- Public class user {
- Private string username;
- Private string password;
- Public String GetUserName (){
- Return username;
- }
- Public void setusername (string username ){
- This. Username = username;
- }
- Public String GetPassword (){
- Return password;
- }
- Public void setpassword (string password ){
- This. Password = password;
- }
- }
Copy code
Finally, the loginactivity judges the login code. Only one login judgment code is pasted:
Java code:
- Private void checkeddata (){
- Username = (edittext) findviewbyid (R. Id. username). gettext (). tostring ();
- Password = (edittext) findviewbyid (R. Id. Password). gettext (). tostring ();
- User user = new user ();
- User. setusername (username );
- User. setpassword (password );
- Loginprotocol login = new loginprotocol ();
- Boolean result = login. checklogin (User );
- If (result) {spidercache. getinstance (). setusersession (User );
- Toast. maketext (getapplicationcontext (), "Logon successful", 1000). Show ();
- Intent intent = new intent ();
- Intent. setclass (loginactivity. This, welcomeactivity. Class );
- Startactivity (intent );
- } Else {toast. maketext (loginactivity. This, "the password or user name does not match. Please enter it again! ", 1000). Show ();
- }
- }
From: http://www.apkbus.com/android-14058-1-3.html