Android collection SSH Server Client creation request

Source: Internet
Author: User
Tags ssh server

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:

  1. <Package name = "login" extends = "JSON-Default">
  2. <Action name = "login" class = "com. jclick. Test. loginaction" method = "login">
  3. <Result type = "JSON"> <paramname = "includeproperties"> result </param> </result>
  4. </Action>
  5. </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:

  1. Package com. jclick. cache;
  2. Import com. jclick. Bean. user;
  3. Public class cache {
  4. Private user;
  5. Private cache (){
  6. }
  7. /** Create a singleton */
  8. Private Static class cacheholder {
  9. Private Static final cache instance = new cache ();
  10. }
  11. Public cache getinstance (){
  12. Return cacheholder. instance;
  13. }
  14. Public user getuser (){
  15. Return user;
  16. }
  17. Public void setuser (User user ){
  18. This. User = user;
  19. }
  20. }

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:

  1. Package com. jclick. Protocol;
  2. Import java. Io. bufferedreader;
  3. Import java. Io. inputstreamreader;
  4. Import java. util. arraylist;
  5. Import java. util. List;
  6. Import org. Apache. http. httpresponse;
  7. Import org. Apache. http. namevaluepair;
  8. Import org. Apache. http. Client. httpclient;
  9. Import org. Apache. http. Client. entity. urlencodedformentity;
  10. Import org. Apache. http. Client. Methods. httppost;
  11. Import org. Apache. http. impl. Client. defaulthttpclient;
  12. Import org. Apache. http. Message. basicnamevaluepair;
  13. Import org. JSON. jsonexception;
  14. Import org. JSON. jsonobject;
  15. Public class baseprotocol {
  16. Private stringbuilder sb = new stringbuilder ();
  17. Private httpclient;
  18. Private httppost httprequest;
  19. Private httpresponse response;
  20. Private list <namevaluepair> namevaluepair = new arraylist <namevaluepair> ();
  21. Baseprotocol (){
  22. Httpclient = new defaulthttpclient ();
  23. }
  24. /**
  25. * Send a request to the server
  26. *
  27. * @ Param URL
  28. * @ Throws exception
  29. */
  30. Protected void pack (string URL) throws exception {
  31. Httpclient = new defaulthttpclient ();
  32. Httprequest = new httppost (URL );
  33. Httprequest. setentity (New urlencodedformentity (namevaluepair ));
  34. Response = httpclient.exe cute (httprequest );
  35. }
  36. /**
  37. * Get the returned data
  38. *
  39. * @ Param URL
  40. * @ Return
  41. * @ Throws exception
  42. */
  43. Protected void parse () throws exception {
  44. // Todo status processing 500 200
  45. If (response. getstatusline (). getstatuscode () = 200 ){
  46. Bufferedreader bufferedreader2 = new bufferedreader (
  47. New inputstreamreader (response. getentity (). getcontent ()));
  48. For (string S = bufferedreader2.readline (); s! = NULL; S = bufferedreader2
  49. . Readline ()){
  50. SB. append (s );
  51. }
  52. }
  53. }
  54. /**
  55. * Send information to the server
  56. *
  57. * @ Param key
  58. * @ Param Value
  59. */
  60. Public void addnamevaluepair (string key, string value ){
  61. Namevaluepair. Add (New basicnamevaluepair (Key, value ));
  62. }
  63. /**
  64. * Returns the jsonobject data model.
  65. *
  66. * @ Return
  67. * @ Throws jsonexception
  68. */
  69. Public jsonobject getjson () throws jsonexception {
  70. Return new jsonobject (sb. tostring ());
  71. }
  72. }

Copy code

Next is the login protocol. Here, the stars are just a class used to simulate login, for your reference only:

Java code:

  1. Package com. jclick. Protocol;
  2. Import org. JSON. jsonobject;
  3. Import com. jclick. Bean. user;
  4. Public class loginprotocol extends baseprotocol {
  5. Private Final Static string url = "http://localhost: 8080/test/login ";
  6. Public Boolean checklogin (User USR ){
  7. Try {
  8. Pack (URL );
  9. Parse ();
  10. Jsonobject OBJ = This. getjson ();
  11. If (obj. getstring ("result"). Equals ("failed ")){
  12. Return false;
  13. } Else {
  14. Return true;
  15. }
  16. } Catch (exception e ){
  17. E. printstacktrace ();
  18. Return false;
  19. }
  20. }
  21. }

Copy code

Then the user entity class is used to save user information:

Java code:

  1. Package com. jclick. Bean;
  2. Public class user {
  3. Private string username;
  4. Private string password;
  5. Public String GetUserName (){
  6. Return username;
  7. }
  8. Public void setusername (string username ){
  9. This. Username = username;
  10. }
  11. Public String GetPassword (){
  12. Return password;
  13. }
  14. Public void setpassword (string password ){
  15. This. Password = password;
  16. }
  17. }

Copy code

Finally, the loginactivity judges the login code. Only one login judgment code is pasted:

Java code:

  1. Private void checkeddata (){
  2. Username = (edittext) findviewbyid (R. Id. username). gettext (). tostring ();
  3. Password = (edittext) findviewbyid (R. Id. Password). gettext (). tostring ();
  4. User user = new user ();
  5. User. setusername (username );
  6. User. setpassword (password );
  7. Loginprotocol login = new loginprotocol ();
  8. Boolean result = login. checklogin (User );
  9. If (result) {spidercache. getinstance (). setusersession (User );
  10. Toast. maketext (getapplicationcontext (), "Logon successful", 1000). Show ();
  11. Intent intent = new intent ();
  12. Intent. setclass (loginactivity. This, welcomeactivity. Class );
  13. Startactivity (intent );
  14. } Else {toast. maketext (loginactivity. This, "the password or user name does not match. Please enter it again! ", 1000). Show ();
  15. }
  16. }
      From: http://www.apkbus.com/android-14058-1-3.html
    Related Article

    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.