1.
The code is as follows |
Copy Code |
Webservice.getvcardbyuserno (String userid,string Userno); |
This is the encapsulated WebService interface.
2. When the interface is invoked in succession twice in a program, KSOAP2 throws an exception when parsing the result returned by the second call.
The exception information is as follows:
The code is as follows |
Copy Code |
org.xmlpull.v1.XmlPullParserException:unexpected type (position:end_document null@1:0 in java.io.inputstreamreader@ 4383BF38)
|
3. This exception does not occur when debugging a interrupt point.
4. Helpless under the use of Android HttpURLConnection directly invoke the WebService interface, direct use will not occur above the exception, so use KSOAP2 access webservice need to set what?
5. Use the HttpURLConnection Access Webserivice code as follows:
(i) Connection WebService
The code is as follows |
Copy Code |
String serverurl= "WebService address"; String soapaction= "Http://www.111cn.net/PhoneClient/GetVcardJson"; String data= ""; String requestdata= "<?xml version=" 1.0 "encoding=" Utf-8 ">rn" + "<soap:envelope xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:xsd=" http://www.w3.org/2001/ XmlSchema "xmlns:soap=" http://schemas.xmlsoap.org/soap/envelope/">rn" + "<soap:header>rn" + "<authheader xmlns=" http://www.111cn.net/PhoneClient/">rn" + "<UserId>" +userid+ "</userid>rn" + "</authheader>rn" + "</soap:header>rn" + "<soap:body>rn" + "<getvcardjson xmlns=" http://www.111cn.net/PhoneClient/">rn" + "<vcardUserNo>" +userno+ "</vcarduserno>rn" + "</getvcardjson>rn" + "</soap:body>rn" + "</soap:Envelope>"; try{ url url =new url (serverurl); HttpURLConnection con= (httpurlconnection) url.openconnection (); Byte[] Bytes=requestdata.getbytes ("Utf-8"); Con.setdoinput (TRUE); Con.setdooutput (TRUE); Con.setusecaches (FALSE); Con.setrequestmethod ("POST"); Con.setrequestproperty ("Content-type", "text/xml;charset=utf-8"); Con.setrequestproperty ("SOAPAction", SOAPAction); Con.setrequestproperty ("Content-length", "" "+bytes.length); OutputStream Outstream=con.getoutputstream (); Outstream.write (bytes); Outstream.flush (); Outstream.close (); InputStream Instream=con.getinputstream (); Data=parser (instream); |
(ii) Resolution of returned data
The code is as follows |
Copy Code |
Private Static String parser (InputStream in) { Xmlpullparser parser=xml.newpullparser (); String data= ""; try{ int flag=0 Parser.setinput (in, utf-8); int eventype=parser.geteventtype (); while (Eventype !=xmlpullparser.end_document) { Switch (eventype) { Case XmlPullParser.START_DOCUMENT:break; Case Xmlpullparser.start_tag: break; Case Xmlpullparser.text: Data=parser.gettext (); break; Case XmlPullParser.END_TAG:break; } Parser.next (); Eventype=parser.geteventtype (); } }catch (xmlpullparserexception e) { E.printstacktrace (); } catch (IOException e) { E.printstacktrace (); } return data; } |