【问题标题】:Getting the right envelope with KSOAP2使用 KSOAP2 获得正确的信封
【发布时间】:2015-03-11 00:14:38
【问题描述】:

我是 KSOAP2 的新手。显然,我无法为 Android 中的 Web 服务调用获取正确类型的信封。

这是我的代码:

        private static final String METHOD_NAME = "wmtdEncApaLuces";
        private static final String SOAP_ACTION = "http://tempuri.org/wsLucesAutomaticas/wmtdEncApaLuces";
        private static final String NAMESPACE = "http://tempuri.org";
        private static final String URL = "http://softour.ddns.net:8083/wsLucesAutomaticas.svc/basic";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo strIp =new PropertyInfo();
        strIp.setName("strIp");
        strIp.setValue("192.168.1.121");
        strIp.setType(String.class);
        request.addProperty(strIp);

        PropertyInfo boolApaEnc =new PropertyInfo();
        boolApaEnc.setName("boolApaEnc");
        boolApaEnc.setValue(false);
        boolApaEnc.setType(boolean.class);
        request.addProperty(boolApaEnc);

        PropertyInfo strDispositivo =new PropertyInfo();
        strDispositivo.setName("strDispositivo");
        strDispositivo.setValue("Nexus 6");
        strDispositivo.setType(String.class);
        request.addProperty(strDispositivo);

        PropertyInfo strUsuario =new PropertyInfo();
        strUsuario.setName("strUsuario");
        strUsuario.setValue("John");
        strUsuario.setType(String.class);
        request.addProperty(strUsuario);

        PropertyInfo intTipoUs =new PropertyInfo();
        intTipoUs.setName("intTipoUs");
        intTipoUs.setValue(1);
        intTipoUs.setType(int.class);
        request.addProperty(intTipoUs);

        PropertyInfo intPuerto =new PropertyInfo();
        intPuerto.setName("intPuerto");
        intPuerto.setValue(85);
        intPuerto.setType(int.class);
        request.addProperty(intPuerto);

        PropertyInfo strIpConexionArduino =new PropertyInfo();
        strIpConexionArduino.setName("strIpConexionArduino");
        strIpConexionArduino.setValue("192.168.1.120");
        strIpConexionArduino.setType(String.class);
        request.addProperty(strIpConexionArduino);

        PropertyInfo intPuertoConexionArduino =new PropertyInfo();
        intPuertoConexionArduino.setName("intPuertoConexionArduino");
        intPuertoConexionArduino.setValue(1234);
        intPuertoConexionArduino.setType(int.class);
        request.addProperty(intPuertoConexionArduino);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            Log.d("myTag", "Request: " + androidHttpTransport.requestDump);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            // Log.d("myTag", "Response: " + response.toString());
        }
        catch(Exception e) {
            e.printStackTrace();
        }

为 SoapSerializationEnvelope 生成的 XML 是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <v:Header />
   <v:Body>
      <n0:wmtdEncApaLuces xmlns:n0="http://tempuri.org" id="o0" c:root="1">
         <strIp i:type="d:string">192.168.1.121</strIp>
         <boolApaEnc i:type="d:boolean">false</boolApaEnc>
         <strDispositivo i:type="d:string">Nexus 6</strDispositivo>
         <strUsuario i:type="d:string">Alonso</strUsuario>
         <intTipoUs i:type="d:int">1</intTipoUs>
         <intPuerto i:type="d:int">85</intPuerto>
         <strIpConexionArduino i:type="d:string">192.168.1.120</strIpConexionArduino>
         <intPuertoConexionArduino i:type="d:int">1234</intPuertoConexionArduino>
      </n0:wmtdEncApaLuces>
   </v:Body>
</v:Envelope>

我还用 SoapUI 对其进行了测试,它生成了这个 XML,这与 KSOAP 生成的不同。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:wmtdEncApaLuces>
         <!--Optional:-->
         <tem:strIp>192.168.1.121</tem:strIp>
         <tem:bolApaEnc>false</tem:bolApaEnc>
         <!--Optional:-->
         <tem:strDispositivo>Nexus 6</tem:strDispositivo>
         <!--Optional:-->
         <tem:strUsuario>John</tem:strUsuario>
         <tem:intTipoUs>1</tem:intTipoUs>
         <tem:intPuerto>85</tem:intPuerto>
         <!--Optional:-->
         <tem:strIpConexionArduino>192.168.1.120</tem:strIpConexionArduino>
         <tem:intPuertoConexionArduino>1234</tem:intPuertoConexionArduino>
      </tem:wmtdEncApaLuces>
   </soapenv:Body>
</soapenv:Envelope>

这是http://softour.ddns.net:8083/wsLucesAutomaticas.svc/basic?singleWSDL的定义

感谢任何帮助。

【问题讨论】:

    标签: android soap ksoap2


    【解决方案1】:

    关于命名空间,看这个:

    <v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    

    现在,看看这个:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    

    命名空间是不同的。尝试在 KSOAP 客户端中添加正确的命名空间,或者更好地在 .NET Web 服务中正确处理请求。

    下一个代码有效,尝试使用 HttPost 而不是 KSOAP:

    HttpPost httppost = new HttpPost(URL);
    StringEntity se;
    String SOAPRequestXML = "";
    try {
        SOAPRequestXML="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body><tem:wmtdEstadoActuadoIluminacion><tem:strIp>192.168.1.121</tem:strIp><tem:strDispositivo>Nexus 6</tem:strDispositivo><tem:strUsuario>Alonso</tem:strUsuario><tem:intTipoUs>1</tem:intTipoUs><tem:intPuerto>85</tem:intPuerto><tem:strIpConexionArduino>192.168.1.120</tem:strIpConexionArduino><tem:intPuertoConexionArduino>1234</tem:intPuertoConexionArduino></tem:wmtdEstadoActuadoIluminacion></soapenv:Body></soapenv:Envelope>";
        se = new StringEntity(SOAPRequestXML, HTTP.UTF_8);
        se.setContentType("text/xml");
        httppost.setHeader("Accept-Charset","utf-8");
        httppost.setHeader("soapaction", SOAP_ACTION);
        httppost.setEntity(se);
    
        HttpClient httpclient = new DefaultHttpClient();
        BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
        HttpEntity resEntity = httpResponse.getEntity();
        Log.e("RESPONSE: ", EntityUtils.toString(resEntity));
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    注意:记住 AsyncTask 以便执行请求。

    【讨论】:

    • 这应该作为评论发布,而不是答案。不幸的是,您还没有代表发表评论,但如果您在其他问题上获得一些支持,您很快就会获得该特权。
    • 感谢您的评论...这是我的第一次。
    • 不客气。不要让反对票让您灰心。赞成票多于抵消反对票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    相关资源
    最近更新 更多