android ksoap2 訪問webservice,連續兩次調用時,第二次調用異常

來源:互聯網
上載者:User

1.Webservice.GetVcardByUserNo(String userId,String userNo);這個是封裝了的webservice介面。
2.在程式中連續兩次調用該介面時,ksoap2在解析第二次調用返回的結果時拋異常。
    異常資訊如下:org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@4383bf38) 
3.打斷點調試時,不會出現該異常。

4.無奈之下使用android 的HttpURLConnection 直接調用webservice介面,直接使用時不會發生以上異常,所以使用ksoap2 訪問webservice需要設定什麼呢?

5.使用HttpUrlConnection訪問webserivice代碼如下:

(一)串連webservice

String ServerUrl="webservice地址";
 String soapAction="http://www.v_card.net.cn/PhoneClient/GetVcardJson";

 String data="";

String requestData="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"+
"<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/\">\r\n"
 +"<soap:Header>\r\n"+
   "<AuthHeader xmlns=\"http://www.v_card.net.cn/PhoneClient/\">\r\n"+
    "<UserId>"+userID+"</UserId>\r\n"+
   "</AuthHeader>\r\n"+
 "</soap:Header>\r\n"+
 "<soap:Body>\r\n"+
   "<GetVcardJson xmlns=\"http://www.v_card.net.cn/PhoneClient/\">\r\n"+
     "<vcardUserNo>"+userNo+"</vcardUserNo>\r\n"+
   "</GetVcardJson>\r\n"+
 "</soap:Body>\r\n"+

"</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);

(二)解析返回的資料

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.