如何解析webservice返回的SoapObject数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何解析webservice返回的SoapObject数据相关的知识,希望对你有一定的参考价值。

SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(METHOD_NAME);
for (int i = 0; i < detail.getPropertyCount(); i++)

SoapObject mstr = (SoapObject) detail.getProperty(i);

String str_result = mstr .getProperty(0).toString();
list.add(str_result); //这个你要的
参考技术A 要看实际类型
~
JAVA 中,一般直接返回JAVA对象,不然就返回XML/JSON

~

webservice返回的xml怎么解析

webservice返回的xml解析方法:

一般来说,调用webService通常需要几个步骤,在调用之前,首先需要下载Soap的jar包。

1、参数设置:上面说到的几个参数都要先设置,这主要依赖于要调用的web'Service的网址:

// 命名空间      

String nameSpace = "http://WebXml.com.cn/";      

// 调用的方法名称      

String methodName = "getDetailInfoByTrainCode";      

// EndPoint      

String endPoint = "http//webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx";      

// SOAP Action      

String soapAction = "http//WebXml.com.cn/getDetailInfoByTrainCode";     

2、指定命名空间与调用方法名

// 指定WebService的命名空间和调用的方法名      

SoapObject rpc = new SoapObject(nameSpace, methodName);    

3、设置参数:

// 设置需调用WebService接口需要传入的两个参数TrainCode、userId      

rpc.addProperty("TrainCode", params[0]);      

rpc.addProperty("UserID","");   

4、生成调用WebService方法的SOAP请求信息

// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本      

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);   

envelope.bodyOut = rpc;   

5、调用WebService方法

try       

    // 调用WebService      

    transport.call(soapAction, envelope);      

 catch (Exception e)       

    e.printStackTrace();      

    

6、解析WebService中的DataSet数据

SoapObject soap1=(SoapObject)object.getProperty("getDetailInfoByTrainCodeResult");  

SoapObject childs=(SoapObject)soap1.getProperty(1);  

SoapObject soap2=(SoapObject)childs.getProperty(0);  

///  

for(int i=0;i<soap2.getPropertyCount();i++)  

SoapObject soap3=(SoapObject)soap2.getProperty(i);  

///  

    Info info=new Info();  

    info.setStation(soap3.getProperty(0).toString());  

    info.setArriveTime(soap3.getProperty(1).toString());  

    info.setStartTime(soap3.getProperty(2).toString());  

    info.setKm(soap3.getProperty(3).toString());  

    Raininfo.add(info);  

//result=soap3.getProperty(3).toString();  

 

数据格式如下:

参考技术A string title = HttpUtility.UrlEncode(txtTitle.Text);
string content = HttpUtility.UrlEncode(content1.Value);

string postData = "title=" + title + "&content=" + content;
byte[] dataBytes = Encoding.UTF8.GetBytes(postData);

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
string url = System.Configuration.ConfigurationManager.AppSettings["KeywordWebService"];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = dataBytes.Length;
request.Method = "POST";

Stream postStream = request.GetRequestStream();
postStream.Write(dataBytes, 0, dataBytes.Length);
postStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

System.Xml.XmlDocument xml = new System.Xml.XmlDocument();

StreamReader receiveStream = new StreamReader(response.GetResponseStream());
string receiveString = receiveStream.ReadToEnd();
sw.Stop();

xml.LoadXml(receiveString);

System.Xml.XmlNodeList keywords = xml.SelectNodes("//Keyword");
string strkeywords = "";
foreach (System.Xml.XmlNode keyword in keywords)

System.Diagnostics.Debug.WriteLine(keyword.InnerText);
strkeywords = strkeywords + keyword.InnerText + " ";


this.txtSearch.Value = strkeywords.Trim();

以上是关于如何解析webservice返回的SoapObject数据的主要内容,如果未能解决你的问题,请参考以下文章

用Java调用webservice,并解析返回值,大神们 求助

webservice返回的xml怎么解析

webservice如何返回json字符串

怎么用Axis2发布一个webservice,service返回json格式数据,可以用Ajax访问直接解析?

急!java调用webservice并解析返回值,重谢!!

java使用POST发送soap报文请求webservice返回500错误解析