KSOAP2-Android addPropery 不起作用

发布于 2024-11-15 11:04:40 字数 6391 浏览 2 评论 0原文

我在 GAE 上做了一个 WS。 (http://testitthanks.appspot.com/)

我想在 Android 上使用 KSOAP2 来获取响应。

这是我的代码:

public class HelloSOAPAndroidClientActivity extends Activity {

//you can get these values from the wsdl file
private static final String SOAP_ACTION = "";
private static final String OPERATION_NAME = "sayHello";
private static final String WSDL_TARGET_NAMESPACE = "http://example.com/";
private static final String SOAP_ADDRESS = "http://testitthanks.appspot.com/hellosoapserver";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textView = new TextView(this);
    setContentView(textView);

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
            OPERATION_NAME);
    request.addProperty("arg0","ONE");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    httpTransport.debug = true;

    try {

        httpTransport.call(SOAP_ACTION, envelope);
        Log.v("TEST", httpTransport.requestDump);
        Log.v("TEST", httpTransport.responseDump);

        Object response = envelope.getResponse();       
        textView.setText(response.toString());


    } catch (Exception exception) {
        textView.setText(exception.toString());
    }
}
}

它应该显示

你好,一号!

但可悲的是它总是看起来像

你好,空!

这是我的 WSDL:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace="http://example.com/" name="GreeterService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://example.com/" schemaLocation="GreeterService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="sayHello">
    <part name="parameters" element="tns:sayHello"/>
  </message>
  <message name="sayHelloResponse">
    <part name="parameters" element="tns:sayHelloResponse"/>
  </message>
  <message name="sayGoodbye">
    <part name="parameters" element="tns:sayGoodbye"/>
  </message>
  <message name="sayGoodbyeResponse">
    <part name="parameters" element="tns:sayGoodbyeResponse"/>
  </message>
  <portType name="Greeter">
    <operation name="sayHello">
      <input message="tns:sayHello"/>
      <output message="tns:sayHelloResponse"/>
    </operation>
    <operation name="sayGoodbye">
      <input message="tns:sayGoodbye"/>
      <output message="tns:sayGoodbyeResponse"/>
    </operation>
  </portType>
  <binding name="GreeterPortBinding" type="tns:Greeter">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="sayHello">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <operation name="sayGoodbye">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="GreeterService">
    <port name="GreeterPort" binding="tns:GreeterPortBinding">
      <soap:address location="http://testitthanks.appspot.com/hellosoapserver"/>
    </port>
  </service>
</definitions>

感谢您的建议!

---

请求转储

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:d="http://www.w3.org/2001/XMLSchema" 
    xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
    <sayHello xmlns="http://example.com/" id="o0" c:root="1">
    <arg0 i:type="d:string">ONE</arg0>
    </sayHello>
    </v:Body>
</v:Envelope>

响应转储

<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
    <ns3:sayHelloResponse xmlns:ns3="http://example.com/">
    <return>Hello, null!</return>
    </ns3:sayHelloResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

---

我的xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://example.com/" xmlns:tns="http://example.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="sayGoodbye" type="tns:sayGoodbye"/>

  <xs:element name="sayGoodbyeResponse" type="tns:sayGoodbyeResponse"/>

  <xs:element name="sayHello" type="tns:sayHello"/>

  <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

  <xs:complexType name="sayHello">
    <xs:sequence>
      <xs:element name="arg0" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sayHelloResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sayGoodbye">
    <xs:sequence>
      <xs:element name="arg0" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sayGoodbyeResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

I made a WS on the GAE. (http://testitthanks.appspot.com/)


And I want to use KSOAP2 on Android to get response.


Here is my code:

public class HelloSOAPAndroidClientActivity extends Activity {

//you can get these values from the wsdl file
private static final String SOAP_ACTION = "";
private static final String OPERATION_NAME = "sayHello";
private static final String WSDL_TARGET_NAMESPACE = "http://example.com/";
private static final String SOAP_ADDRESS = "http://testitthanks.appspot.com/hellosoapserver";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textView = new TextView(this);
    setContentView(textView);

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
            OPERATION_NAME);
    request.addProperty("arg0","ONE");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    httpTransport.debug = true;

    try {

        httpTransport.call(SOAP_ACTION, envelope);
        Log.v("TEST", httpTransport.requestDump);
        Log.v("TEST", httpTransport.responseDump);

        Object response = envelope.getResponse();       
        textView.setText(response.toString());


    } catch (Exception exception) {
        textView.setText(exception.toString());
    }
}
}

It should show

Hello, ONE!

But sadly it always looks like

Hello, null!

Here is my WSDL:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace="http://example.com/" name="GreeterService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://example.com/" schemaLocation="GreeterService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="sayHello">
    <part name="parameters" element="tns:sayHello"/>
  </message>
  <message name="sayHelloResponse">
    <part name="parameters" element="tns:sayHelloResponse"/>
  </message>
  <message name="sayGoodbye">
    <part name="parameters" element="tns:sayGoodbye"/>
  </message>
  <message name="sayGoodbyeResponse">
    <part name="parameters" element="tns:sayGoodbyeResponse"/>
  </message>
  <portType name="Greeter">
    <operation name="sayHello">
      <input message="tns:sayHello"/>
      <output message="tns:sayHelloResponse"/>
    </operation>
    <operation name="sayGoodbye">
      <input message="tns:sayGoodbye"/>
      <output message="tns:sayGoodbyeResponse"/>
    </operation>
  </portType>
  <binding name="GreeterPortBinding" type="tns:Greeter">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="sayHello">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <operation name="sayGoodbye">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="GreeterService">
    <port name="GreeterPort" binding="tns:GreeterPortBinding">
      <soap:address location="http://testitthanks.appspot.com/hellosoapserver"/>
    </port>
  </service>
</definitions>

Thanks for any advice!

---

requestDump

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:d="http://www.w3.org/2001/XMLSchema" 
    xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
    <sayHello xmlns="http://example.com/" id="o0" c:root="1">
    <arg0 i:type="d:string">ONE</arg0>
    </sayHello>
    </v:Body>
</v:Envelope>

responseDump

<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
    <ns3:sayHelloResponse xmlns:ns3="http://example.com/">
    <return>Hello, null!</return>
    </ns3:sayHelloResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

---

My xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://example.com/" xmlns:tns="http://example.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="sayGoodbye" type="tns:sayGoodbye"/>

  <xs:element name="sayGoodbyeResponse" type="tns:sayGoodbyeResponse"/>

  <xs:element name="sayHello" type="tns:sayHello"/>

  <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

  <xs:complexType name="sayHello">
    <xs:sequence>
      <xs:element name="arg0" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sayHelloResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sayGoodbye">
    <xs:sequence>
      <xs:element name="arg0" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sayGoodbyeResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2024-11-22 11:04:40

终于我修好了!!!!

问题发生是因为......

envelope.dotNet = true;

但是,我使用“GAE”而不是“.NET”,所以上面的代码应该更改为

envelope.dotNet = false;

Finally, I fixed it!!!!

The problem happend because...

envelope.dotNet = true;

However, I use "GAE" not ".NET", so the above code should change to

envelope.dotNet = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文