Android_webservices_ Source Code Analysis

Source: Internet
Author: User
Tags response code

this blog post for the original, reproduced please specify the source! http://blog.csdn.net/zimo2013/article/details/38037989in the Android_webservices_ introduction article, the basic knowledge of webservices is briefly introduced. The following main analysis Ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar implement the source code. 1. Call the webservices process

public void Getremoteinfo (string phonesec) {string nameSpace = "http://WebXml.com.cn/"; String methodName = "Getmobilecodeinfo"; String endPoint = "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; String soapaction = "http://WebXml.com.cn/getMobileCodeInfo";//1. Initialize the Soapobject object. Sets the number of parameters for the method. Equivalent to the body soapobject request = new Soapobject (NameSpace, methodName); Request.addproperty ("Mobilecode", phonesec); Request.addproperty ("UserId", "");//2. Instantiate the Soapserializationenvelope object, equivalent to Chimpi soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER10); envelope.bodyout = Request;envelope.dotnet = true;// Compatible with. NET Development net-services//3. Instantiate the Httptransportse object, and you can specify the request time Httptransportse transport = new Httptransportse ( EndPoint);//httptransportse transport = new Httptransportse (endPoint, timeout); try {//4. Core method Invocation, Among them, Soapacton is invalid at Soapserializationenvelope.ver12, and Transport.call for POST request (SOAPAction, envelope); Soapobject response = (soapobject) envelope.bodyin;final String result = responsE.getproperty (0). toString ();//vectortoast (result);} catch (Exception e) {e.printstacktrace (); Toast (E.getmessage ());}}

2.transport.call Key source code Analysis

/** * Perform A SOAP call with a given namespace and the given envelope providing * any extra headers that the user Requir Es such as cookies. Headers that is * returned by the Web service is returned to the caller in the form of a * <code>list</code > of <code>HeaderProperty</code> instances. * * @param SOAPAction * The namespace with which to perform the call in. * @param envelope * The env Elope the contains the information for the call. * @param headers * <code>List</code> of <code>HeaderProperty</code> headers to send with the SOA P request. * @param outputFile * A file to stream the response into rather than parsing it, streaming happens when file IS isn't null * * @return Headers returned by the Web service as a <code>List</code> of * <code>headerprop Erty</code> instances. * * @throws httpresponseexception * An ioexception when Http response code is different From $ */public List call (String soapaction, SoapEnvelope envelope, list headers, File outputFile) throws Httprespon    Seexception, IOException, xmlpullparserexception {if (soapaction = = null) {soapaction = "\" \ "";    }//According to envelope, serialize it to a request byte array byte[] RequestData = createrequestdata (envelope, "UTF-8"); Debug=true, the value of Requestdump is the requested data, convenient debugging Requestdump = Debug?

New String (requestData): null; Responsedump = null; Connection = new Serviceconnectionse (proxy, URL, timeout), including set time serviceconnection connection = Getserviceconnection ( ); Connection.setrequestproperty ("User-agent", user_agent); SOAPAction is not a valid headers for VER12 so does not add//IT//@see "http://code.google.com/p/ksoap2-android/i Ssues/detail?

id=67 if (envelope.version! = soapserializationenvelope.ver12) {connection.setrequestproperty ("SOAPAction", SOA Paction); } if (envelope.version = = soapserializationenvelope.ver12) {connection.setrequestproperty ("Content-Type", CONTE NT_TYPE_SOAP_XML_CHARSET_UTF_8); } else {connection.setrequestproperty ("Content-type", content_type_xml_charset_utf_8); }//This seems-cause issues so we are removing it//connection.setrequestproperty ("Connection", "close"); Connection.setrequestproperty ("accept-encoding", "gzip"); Pass the headers provided by the user along with the call if (headers! = null) {for (int i = 0; i < Heade Rs.size (); i++) {Headerproperty hp = (headerproperty) headers.get (i); Connection.setrequestproperty (Hp.getkey (), Hp.getvalue ()); }}//Post request Connection.setrequestmethod ("post"); Send data. Takes a long time to send requestData to the connection output stream SendData (RequestData, connection, EnveloPE); RequestData = null; InputStream is = null; List retheaders = null; byte[] buf = null; To allow releasing the resource after used int contentlength = 8192; To determine the size of the response and adjust buffer size Boolean gzippedcontent = false; Boolean xmlcontent = false; Get the response code int status = Connection.getresponsecode (); try {//Get response header Retheaders = Connection.getresponseproperties (); for (int i = 0; i < retheaders.size (); i++) {Headerproperty hp = (headerproperty) retheaders.get (i); HTTP response code has a null key if (null = = Hp.getkey ()) {continue; }//If We know the size of the response, we should use the size to initiate VARs If (Hp.getkey (). eq Ualsignorecase ("Content-length")) {if (hp.getvalue () = null) {try { ContentLength = Integer.parseint (Hp.getvalue ()); } catch (NumberFormatException nfe) {contentlength = 8192; }}}//Check the Content-type header to see if we ' re getting back XML, in case of a SOAP Fault on Codes if (Hp.getkey (). Equalsignorecase ("Content-type") &amp ;& Hp.getvalue (). Contains ("xml")) {xmlcontent = true; }//Ignoring case since users found, all smaller case is used on some server//and even if it Is wrong according to spec, we rather has it work. if (Hp.getkey (). Equalsignorecase ("content-encoding") && Hp.getvalue (). Equalsignorecase ("gzip")) { Gzippedcontent = true; }}//first Check the response code .... if (status! =) {//throw new IOException ("HTTP Request failed, HTTP status: "+ status"; throw new HttpresponseeXception ("HTTP request failed, HTTP status:" + status, status); } if (ContentLength > 0) {if (gzippedcontent) {is = Getunzippedinputstream ( New Bufferedinputstream (Connection.openinputstream (), contentlength)); } else {is = new Bufferedinputstream (Connection.openinputstream (), contentlength); }}} catch (IOException e) {if (ContentLength > 0) {if (gzippedcontent) { is = Getunzippedinputstream (new Bufferedinputstream (Connection.geterrorstream (), contentlength)); } else {is = new Bufferedinputstream (Connection.geterrorstream (), contentlength); }} if (e instanceof httpresponseexception) {if (!xmlcontent) {if (Debug && Amp is! = null) {//go ahead and read the error stream into the debug buffers/file if needed. Readdebug (IS, contentlength, outputFile); }//we never want to drop through to attempting to parse the HTTP error stream as a SOAP response. Connection.disconnect (); Throw e; }}}//Debug=true responsedump= response data for easy debugging if (Debug) {is = Readdebug (IS, ContentLength, Outputfil e); }//Based on the is stream, stream data is parsed into Envelope.bodyin parseresponse (envelope, is,retheaders); Release resource is = NULL; BUF = null; Connection.disconnect (); connection = null; Returns the response header return retheaders;}

Example Download http://download.csdn.net/detail/strawberry2013/7663399

Android_webservices_ Source Code Analysis

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.