【问题标题】:parsing soap response without using ksoap in android解析soap响应而不在android中使用kso​​ap
【发布时间】:2015-05-26 10:28:23
【问题描述】:

为了从 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 是一个肥皂字符串,从服务器得到响应,但是如何解析肥皂响应,因为我没有使用 HttpTransportSEksoap 我不有一个肥皂对象作为响应。

  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>

【问题讨论】:

    标签: android soap wsdl


    【解决方案1】:

    我们可以将 Soap 响应解析为解析 xml。使用 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();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      相关资源
      最近更新 更多