Android uses http get, post, and httpclient to submit text data to the service.
Source: Internet
Author: User
/*** Http request* @ Author kesenhoo**/PublicClassHttprequest{PublicStaticBooleanSendxml (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){ReturnTrue;}ReturnFalse;}/*** Submit parameters to the server through get* @ Param path* @ Param Params* @ Param ENC* @ Return* @ Throws exception*/PublicStaticBooleanSendgetrequest (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 objectStringbuilder sb =NewStringbuilder (PATH );SB. append ('? ');// Iterative MapFor(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 linkURL 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){ReturnTrue;}ReturnFalse;}/*** Submit parameters to the server through post* @ Param path* @ Param Params* @ Param ENC* @ Return* @ Throws exception*/PublicStaticBooleanSendpostrequest (string path, Map <string, string> Params, string ENC)ThrowsException{// The string format to be constructed is as follows:// Title = dsfdsf & timelength = 23 & method = saveStringbuilder sb =NewStringbuilder ();// If the parameter is not blankIf(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 objectByte[] 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: 38Conn. 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 streamOutstream. Write (entitydata );// Fl data in the memoryOutstream. Flush ();Outstream. Close ();// If the request response code is 200, the request is successful.If(Conn. getresponsecode () =200){ReturnTrue;}ReturnFalse;}/*** 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*/PublicStaticBooleanSendrequestfromhttpclient (string path, Map <string, string> Params, string ENC)ThrowsException{// Put the parameter in namevaluepairList <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 dataUrlencodedformentity entitydata =NewUrlencodedformentity (parampairs, ENC );// Construct a Request PathHttppost post =NewHttppost (PATH );// Set the Request EntityPost. setentity (entitydata );// Browser objectDefaulthttpclient client =NewDefaulthttpclient ();// Execute the POST requestHttpresponse response = client.exe cute (post );// Obtain the status code from the status line to determine whether the response code meets the requirementsIf(Response. getstatusline (). getstatuscode () =200){ReturnTrue;}ReturnFalse;}}
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