如何直接soap字符串,访问webservice
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1.Webservice.GetVcardByUserNo(String userId,String userNo);這個是封裝了的webservice接口。
2.在程序中連續(xù)兩次調(diào)用該接口時,ksoap2在解析第二次調(diào)用返回的結(jié)果時拋異常。
? ? 異常信息如下:org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@4383bf38)?
3.打斷點調(diào)試時,不會出現(xiàn)該異常。
4.無奈之下使用android 的HttpURLConnection 直接調(diào)用webservice接口,直接使用時不會發(fā)生以上異常,所以使用ksoap2 訪問webservice需要設(shè)置什么呢?
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);
(二)解析返回的數(shù)據(jù)
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;
}
轉(zhuǎn)載于:https://my.oschina.net/davidjianl/blog/36816
總結(jié)
以上是生活随笔為你收集整理的如何直接soap字符串,访问webservice的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 车辆运动控制(2)车辆横摆动力学建模
- 下一篇: 收缩临时表空间