android - 在android中不使用kso​​ap解析soap响应

标签 android soap wsdl

使用以下方法从 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 响应,因为我没有使用 HttpTransportSEksoap 我不有一个 SOAP 对象作为响应。

  1. 这是从 android 访问 wsdl 服务的正确方法吗?
  2. 我可以将 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 标签。认为他们不需要 HttpTransportSEksoap

  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中不使用kso​​ap解析soap响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30455920/

相关文章:

android - 适用于 Android 的 C++ Builder WSDL 客户端

android - 如何用另一个替换 Android 库项目 list 中的 Activity

android - 长按 paytm 和 swiggy 等 android 应用程序图标后如何显示菜单选项?

Android 键盘调整调整大小

android - 如何修复第三方 sdk 中重复的 libgnuSTL_shared.so 文件?

java - 身份验证 Spring SOAP 到 Workday

java - 找不到所需的类(javax.activation.DataHandler 和 javax.mail.internet.MimeMultipart)。附件支持已禁用

java - 从 java 调用 Soap Webservice(用 C# 编写)

java - 使用 WSDL 的 Web 服务代理类

web-services - Mule CXF SOAP 服务 - 针对 XSD 进行验证并发送自定义响应而不是 Soap 错误