Android uses http get, post, and httpclient to submit text data to the service.

Source: Internet
Author: User
/**  * Http request  * @ Author kesenhoo  *  */ Public ClassHttprequest { Public Static BooleanSendxml (string path, string XML)ThrowsException { Byte[] DATA = xml. getbytes (); URL url =NewURL (PATH ); Httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmethod ("Post"); Conn. setconnecttimeout (5*1000); // If you submit data through post, you must set to allow external data output. Conn. setdooutput (True); Conn. setrequestproperty ("Content-Type","Text/XML; charset = UTF-8"); Conn. setrequestproperty ("Content-Length", String. valueof (data. Length )); Outputstream outstream = conn. getoutputstream (); Outstream. Write (data ); Outstream. Flush (); Outstream. Close (); If(Conn. getresponsecode () =200) { Return True; } Return False; } /**  * Submit parameters to the server through get  * @ Param path  * @ Param Params  * @ Param ENC  * @ Return  * @ Throws exception  */ Public Static BooleanSendgetrequest (string path, Map <string, string> Params, string ENC)ThrowsException { // Construct a string in the following format. The strings here are different based on the situation. //? Method = Save & Title = 435435435 & timelength = 89 & // Use the stringbuilder object Stringbuilder sb =NewStringbuilder (PATH ); SB. append ('? '); // Iterative Map For(Map. Entry <string, string> entry: Params. entryset ()) { SB. append (entry. getkey (). append ('=') . Append (urlencoder. encode (entry. getvalue (), ENC). append ('&'); } SB. deletecharat (sb. Length ()-1); // Open the link URL url =NewURL (sb. tostring ()); Httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmethod ("Get"); Conn. setconnecttimeout (5*1000); // If the request response code is 200, the request is successful. If(Conn. getresponsecode () =200) { Return True; } Return False; } /**  * Submit parameters to the server through post  * @ Param path  * @ Param Params  * @ Param ENC  * @ Return  * @ Throws exception  */ Public Static BooleanSendpostrequest (string path, Map <string, string> Params, string ENC)ThrowsException { // The string format to be constructed is as follows: // Title = dsfdsf & timelength = 23 & method = save Stringbuilder sb =NewStringbuilder (); // If the parameter is not blank If(Params! =Null&&! Params. isempty ()) { For(Map. Entry <string, string> entry: Params. entryset ()) { // If a parameter is submitted in post mode, the content type and length cannot be omitted. SB. append (entry. getkey (). append ('=') . Append (urlencoder. encode (entry. getvalue (), ENC). append ('&'); } SB. deletecharat (sb. Length ()-1); } // Obtain the binary data of the object Byte[] Entitydata = sb. tostring (). getbytes (); URL url =NewURL (PATH ); Httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmethod ("Post"); Conn. setconnecttimeout (5*1000); // If you submit data through post, you must set to allow external data output. Conn. setdooutput (True); // Only header fields of content type and content length are set here // Content-Type: Application/X-WWW-form-urlencoded // Content Length: Content-Length: 38 Conn. setrequestproperty ("Content-Type","Application/X-WWW-form-urlencoded"); Conn. setrequestproperty ("Content-Length", String. valueof (entitydata. Length )); Outputstream outstream = conn. getoutputstream (); // Write the object data into the output stream Outstream. Write (entitydata ); // Fl data in the memory Outstream. Flush (); Outstream. Close (); // If the request response code is 200, the request is successful. If(Conn. getresponsecode () =200) { Return True; } Return False; } /**  * It is much easier to use httpclient in case of HTTPS security mode or cookie operations.  * Use httpclient (open-source project) to submit parameters to the server  * @ Param path  * @ Param Params  * @ Param ENC  * @ Return  * @ Throws exception  */ Public Static BooleanSendrequestfromhttpclient (string path, Map <string, string> Params, string ENC)ThrowsException { // Put the parameter in namevaluepair List <namevaluepair> parampairs =NewArraylist <namevaluepair> (); If(Params! =Null&&! Params. isempty ()) { For(Map. Entry <string, string> entry: Params. entryset ()) { Parampairs. Add (NewBasicnamevaluepair (entry. getkey (), entry. getvalue ())); } } // Encode the request parameters to obtain the object data Urlencodedformentity entitydata =NewUrlencodedformentity (parampairs, ENC ); // Construct a Request Path Httppost post =NewHttppost (PATH ); // Set the Request Entity Post. setentity (entitydata ); // Browser object Defaulthttpclient client =NewDefaulthttpclient (); // Execute the POST request Httpresponse response = client.exe cute (post ); // Obtain the status code from the status line to determine whether the response code meets the requirements If(Response. getstatusline (). getstatuscode () =200) { Return True; } Return False; } }
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.