使用SOAP从Android调用Web服务

时间:2013-10-17 07:12:56

标签: java android web-services ksoap2 android-ksoap2

我正在使用java使用web服务,我正在使用下面的WSDL,但我对SOAP_ACTION感到困惑:

NAMESPACE:

URL:

METHOD_NAME:

WSDL

<wsdl:definitions 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soapjms="http://www.w3.org/2010/soapjms/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://cloudx623.company.com:5555/ws/Monish:TestWebservice/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"name="TestWebservice"
targetNamespace="http://cloudx623.company.com:5555/ws/Monish:TestWebservice/myTest">
<wsdl:types>
 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:tns="http://localhost:8080/Monish/TestWebservice"
 targetNamespace="http://localhost:8080/Monish/TestWebservice">
   <xsd:element name="myTest" type="tns:myTest"/>
   <xsd:element name="myTestResponse" type="tns:myTestResponse"/>
 <xsd:complexType name="myTest">
   <xsd:sequence>
    <xsd:element name="a" nillable="true" type="xsd:string"/>
    <xsd:element name="b" nillable="true" type="xsd:string"/>
   </xsd:sequence>
   </xsd:complexType>
 <xsd:complexType name="myTestResponse">
 <xsd:sequence>
<xsd:element name="output" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
 <wsdl:message name="TestWebservice_PortType_myTestResponse">
<wsdl:part name="parameters" element="tns:myTestResponse"></wsdl:part>
</wsdl:message>
<wsdl:message name="TestWebservice_PortType_myTest">
<wsdl:part name="parameters" element="tns:myTest"></wsdl:part>
</wsdl:message>
<wsdl:portType name="TestWebservice_PortType">
<wsdl:operation name="myTest">
<wsdl:input message="tns:TestWebservice_PortType_myTest"></wsdl:input>
<wsdl:output message="tns:TestWebservice_PortType_myTestResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Monish_TestWebservice_Binder" type="tns:TestWebservice_PortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="myTest">
<soap:operation soapAction="Monish_TestWebservice_Binder_myTest" style="document"/>
<wsdl:input>
<soap:body parts="parameters" use="literal"/>
</wsdl:input>
<wsdl:output>
 <soap:body parts="parameters" use="literal"/>
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
<wsdl:service name="TestWebservice">
<wsdl:port name="Monish_TestWebservice_Port" binding="tns:Monish_TestWebservice_Binder">
 <soap:address location="http://192.28.50.46:5555/ws/Monish:TestWebservice/Monish_TestWebservice_Port"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

使用kso​​ap2的Java代码如下

private static final String SOAP_ACTION = "http://cloudx623.company.com:5555/ws/Monish:TestWebservice/myTest";
private static final String METHOD_NAME = "myTest";
private static final String NAMESPACE = "Monish_TestWebservice_Binder_myTest";
// !!!!! IMPORTANT!!!!! THE URL OF THE CoLDFUSION WEBSERVER NOT LOCALHOST BECAUSE LOCALHOST IS THE ANDROID EMULATOR !!!!!
private static final String URL = "http://192.28.50.46:5555/ws/Monish:TestWebservice?WSDL";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    //CALL the web service method with the two parameters vname and nname
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("a", "3");
            request.addProperty("b", "4");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    envelope.setOutputSoapObject(request);


    HttpTransportSE androidHttpTransport = new HttpTransportSE (URL);                    
    try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            // Get the SAOP Envelope back and the extract the body
            SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;

            Vector Vec = (Vector) resultsRequestSOAP.getProperty("getMessageReturn");

            //Count of the arrays beneath starting from 0
            //You can see the buildup with the php site with nusoap http://localhost/DA/nusoapclient_test2.php
            int testat = Vec.size();

            // It depends on how many arrays we  have we can get to the attributs of one of them with get(0), get(1) ....
            SoapObject test = (SoapObject) Vec.get(0);
            System.out.println(envelope.getResponse());
            //Get the attributes in the array
            String tem = (String) test.getProperty("a");
            tem = tem + " " + (String) test.getProperty("b");

            //Just show it in a text area field called lblStatus
            ((TextView)findViewById(R.id.lblStatus)).setText(tem.toString());  



            // with androidhttptransport you need a catch block

    } catch(Exception E) {
            ((TextView)findViewById(R.id.lblStatus)).setText("ERROR:" + E.getClass().getName() + ": " + E.getMessage());
    }

}

请告诉我这是什么问题。我在Android中收到HTTP错误状态500

2 个答案:

答案 0 :(得分:2)

我认为你需要达到这样的目标 你可以从这里看到完整的例子。

Part - 1 : Calling Web Service from Android

Part - 2: Webservice running in Android with Database

我希望这会对你有所帮助。

答案 1 :(得分:0)

确保您使用正确的命名空间或soap操作。当您使用错误时,会发生该错误。