使用以下方法从 android 访问 wsdl 服务
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(BASIC_URL);
try {
StringEntity se = new StringEntity(SoapRequest, HTTP.UTF_8);
se.setChunked(true);
se.setContentType("text/xml");
httpPost.addHeader("Accept-Encoding", "gzip,deflate");
httpPost.addHeader("SOAPAction", SoapAction);
httpPost.addHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.addHeader(header);
httpPost.setEntity(se);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity resEntity = httpResponse.getEntity();
// response = EntityUtils.toString(resEntity);
return httpResponse;
} catch (Exception e) {
}
其中 SoapRequest 是一个 soap 字符串,得到了服务器的响应,但是如何解析 soap 响应,因为我没有使用 HttpTransportSE 和 ksoap 我不有一个 SOAP 对象作为响应。
- 这是从 android 访问 wsdl 服务的正确方法吗?
- 我可以将 soap 对象转换为 xml 或 json 然后解析吗
示例响应是
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org /soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetResponse xmlns="http://tempuri.org/">
<GetResult>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="First">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstElement" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<NewDataSet xmlns="">
<Exception diffgr:id="Exception1" msdata:rowOrder="0">
<ex_id>12</ex_id>
</Exception>
<Second diffgr:id="Second" msdata:rowOrder="0">
<SecondElement>66</SecondElement>
</Second>
</NewDataSet>
</GetResult>
</GetResponse>
最佳答案
我们可以像解析 xml 一样解析 Soap 响应。使用 xmlpull 解析器,我们可以提取 xml 标签。认为他们不需要 HttpTransportSE 和 ksoap
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES
, false);
myparser.setInput(new StringReader("Soap response here"));
parseXML(myparser);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
public void parseXML(XmlPullParser myParser) {
int event;
String text=null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("ex_id")){
Log.i("----", text);
}
break;
}
event = myParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
关于android - 在android中不使用ksoap解析soap响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30455920/