4 ways Android sends data to Web server

Source: Internet
Author: User

  1. /**
  2. * Two ways to submit data to a Web server in Android four ways
  3. */
  4. Public class Submitdatabyhttpclientandordinaryway {
  5. /**  
  6. * Use GET request to submit data in normal way
  7. * @param map to pass in the data, in the form of a map encapsulated
  8. * @param path requires the address of the server servlet
  9. * @return The parameters of the Boolean type returned
  10. * @throws Exception
  11. */
  12. Public Boolean Submitdatabydoget (map<string, string> Map, String Path) throws Exception {
  13. //Patchwork a request address
  14. StringBuilder sb = new StringBuilder (path);
  15. Sb.append ("?");
  16. for (map.entry<string, string> entry:map.entrySet ()) {
  17. Sb.append (Entry.getkey ()). Append ("="). Append (Entry.getvalue ());
  18. Sb.append ("&");
  19. }
  20. Sb.deletecharat (Sb.length ()- 1);
  21. String str = sb.tostring ();
  22. System.out.println (str);
  23. URL url = new URL (str);
  24. HttpURLConnection httpconn = (httpurlconnection) url.openconnection ();
  25. Httpconn.setrequestmethod ("GET");
  26. Httpconn.setreadtimeout (5000);
  27. if (httpconn.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {
  28. return true;
  29. }
  30. return false;
  31. }
  32. /**  
  33. * Dopost request submission data in normal mode
  34. * @param map to pass in the data, in the form of a map encapsulated
  35. * @param path requires the address of the server servlet
  36. * @return The parameters of the Boolean type returned
  37. * @throws Exception
  38. */
  39. Public Boolean Submitdatabydopost (map<string, string> Map, String Path) throws Exception {
  40. //Note that there is no parameter in the post address, so be careful not to add the following parameters when Newurl
  41. URL url = new URL (path);
  42. //Post method when the parameter and URL are submitted separately, the Parameter form is like this: name=y&age=6
  43. StringBuilder sb = new StringBuilder ();
  44. //Sb.append ("?");
  45. for (map.entry<string, string> entry:map.entrySet ()) {
  46. Sb.append (Entry.getkey ()). Append ("="). Append (Entry.getvalue ());
  47. Sb.append ("&");
  48. }
  49. Sb.deletecharat (Sb.length ()- 1);
  50. String str = sb.tostring ();
  51. HttpURLConnection httpconn = (httpurlconnection) url.openconnection ();
  52. Httpconn.setrequestmethod ("POST");
  53. Httpconn.setreadtimeout (5000);
  54. Httpconn.setdooutput (true);
  55. Httpconn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
  56. Httpconn.setrequestproperty ("Content-length", String.valueof (Str.getbytes (). Length));
  57. OutputStream OS = Httpconn.getoutputstream ();
  58. Os.write (Str.getbytes ());
  59. if (httpconn.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**  
  65. * Send data to the server in HttpClient doget mode
  66. * @param map to pass in the data, in the form of a map encapsulated
  67. * @param path requires the address of the server servlet
  68. * @return The parameters of the Boolean type returned
  69. * @throws Exception
  70. */
  71. Public Boolean Submitdatabyhttpclientdoget (map<string, string> Map, String Path) throws Exception { /c4>
  72. HttpClient HC = new defaulthttpclient ();
  73. //Request Path
  74. StringBuilder sb = new StringBuilder (path);
  75. Sb.append ("?");
  76. for (map.entry<string, string> entry:map.entrySet ()) {
  77. Sb.append (Entry.getkey ()). Append ("="). Append (Entry.getvalue ());
  78. Sb.append ("&");
  79. }
  80. Sb.deletecharat (Sb.length ()- 1);
  81. String str = sb.tostring ();
  82. System.out.println (str);
  83. HttpGet request = new HttpGet (sb.tostring ());
  84. HttpResponse response = Hc.execute (request);
  85. if (Response.getstatusline (). Getstatuscode () = = HTTPURLCONNECTION.HTTP_OK) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. /**  
  91. * Submit data to the server in HttpClient Dopost way
  92. * @param map to pass in the data, in the form of a map encapsulated
  93. * @param path requires the address of the server servlet
  94. * @return The parameters of the Boolean type returned
  95. * @throws Exception
  96. */
  97. Public Boolean Submintdatabyhttpclientdopost (map<string, string> Map, String Path) throws Exception {
  98. ///1. Obtain a httpclient equivalent to the browser object, using the implementation class of this interface to create the object, Defaulthttpclient
  99. HttpClient HC = new defaulthttpclient ();
  100. ///Dopost request is set at the time of request, the key is the path
  101. HttpPost request = new HttpPost (path);
  102. //2. Set the request parameters for the request, which is the parameter that will be uploaded to the Web server
  103. list<namevaluepair> parameters = new arraylist<namevaluepair> ();
  104. for (map.entry<string, string> entry:map.entrySet ()) {
  105. Namevaluepair namevaluepairs = new Basicnamevaluepair (Entry.getkey (), Entry.getvalue ());
  106. Parameters.Add (Namevaluepairs);
  107. }
  108. //Request entity httpentity is also an interface, we use its implementation class urlencodedformentity to create objects, note that later a string type of argument is used to specify the encoding of the
  109. httpentity entity = new urlencodedformentity (Parameters, "UTF-8");
  110. Request.setentity (entity);
  111. //3. Execution Request
  112. HttpResponse response = Hc.execute (request);
  113. //4. To determine the success of a request by a return code
  114. if (Response.getstatusline (). Getstatuscode () = = HTTPURLCONNECTION.HTTP_OK) {
  115. return true;
  116. }
  117. return false;
  118. }
  119. }

Original from http://luecsc.blog.51cto.com/2219432/1111923

4 ways Android sends data to Web server

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.