Android KSOAP2 Access WebService, second call exception (turn) twice

Source: Internet
Author: User
Tags soap

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;
}

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.