使用kso​​ap2的SOAP

时间:2012-09-21 08:00:18

标签: android soap ksoap2

尝试从android调用SOAP服务时遇到问题。我谷歌,我发现我应该使用kso​​ap2。所以我安装了那个库并获得了一些代码......

 String METHOD_NAME = "name";
      String SOAP_ACTION = "url/name";
     String NAMESPACE = "url";
      String URL = "url";
    //you can get these values from the wsdl file^

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
        request.addProperty("user", "user");
        request.addProperty("pass", "pass");
        //variable name, value. I got the variable name, from the wsdl file!
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
        envelope.setOutputSoapObject(request);  //prepare request

        List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();

        headerList.add(new HeaderProperty("Content-Type", "text/xml; charset=utf-8"));
        headerList.add(new HeaderProperty("SoapAction", "url/name"));                   
        HttpTransportSE httpTransport = new HttpTransportSE(URL);  
        httpTransport.debug = true; 
        try {
            httpTransport.call(SOAP_ACTION, envelope, headerList);
            SoapObject result=(SoapObject)envelope.getResponse();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (XmlPullParserException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

并在XmlPullParserException e2中抛出一个异常,名为:

 org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html>@2:7 in java.io.InputStreamReader@406da6b8)

所以现在我不知道如何让它发挥作用。我认为Android的管理效果非常差,因为在iphone上我可以在几分钟内完成它。

顺便说一句,如果有可能帮助那个应该使用的信封:

 <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
            "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tns=\"url\" " +
            "xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" " +
            "xmlns:soap-enc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" >" +
            "<SOAP-ENV:Body><mns:name xmlns:mns=\"url\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
            "<user xsi:type=\"xsd:string\">user</user>" +
            "<pass xsi:type=\"xsd:string\">pass</pass>" +
            "</mns:name></SOAP-ENV:Body></SOAP-ENV:Envelope>

任何helkp都会受到关注,因为我现在试图让它工作很长一段时间......

也许这有助于预期结果如下:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:MPListResponse><return xsi:type="xsd:string">[{"tip":"123456","stevilka":"123456789","key":"some value","zacetek":"01.05.2007","potek":"01.01.2018","axa":0},]</return></ns1:MPListResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

这是我的httptransport请求:

 Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.w3.org/2003/05/soap-encoding" xmlns:v="http://www.w3.org/2003/05/soap-envelope"><v:Header /><v:Body><n0:name id="o0" c:root="1" xmlns:n0="url"><user i:type="d:string">user</user><pass i:type="d:string">pass</pass></n0:MPList></v:Body></v:Envelope>

2 个答案:

答案 0 :(得分:0)

wsdl2ksoap可以在这里帮助你,如果你的xml响应不是太长,那么使用java SAXparser。至少这是我成功的方法。

答案 1 :(得分:0)

也许你的SOAP Envelope版本错了。尝试改变它。这对我有用。

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);