2019独角兽企业重金招聘Python工程师标准>>>
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;
}