从android调用Web服务

时间:2011-11-23 06:20:46

标签: android web-services axis

我使用this tutorial创建了一项网络服务。我已经成功创建了Web服务客户端。它对我来说很好。现在我想从android调用这个web服务。我试了两天没有任何结果。请帮我解决这个问题。

private static String SOAP_ACTION = "http://localhost:8080/MyFirstWebService/services/FirstWebService";

    private static String NAMESPACE = "http://localhost:8080/MyFirstWebService/services/";
    private static String METHOD_NAME = "addTwoNumbers";

    private static String URL = "http://localhost:8080/MyFirstWebService/services/FirstWebService?wsdl";


     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Initialize soap request + add parameters
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);        
         request.addProperty("firstNumber",""+5);
         request.addProperty("secondNumber",""+5);

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

        // Make the soap call.
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {

            //this is the actual part that will call the webservice
            androidHttpTransport.call(SOAP_ACTION, envelope);        
        } catch (Exception e) {
            e.printStackTrace(); 
        }

        // Get the SoapResult from the envelope body.       
        SoapObject result = (SoapObject)envelope.bodyIn;
        Log.d("prabhu","result  is ....."+result);

        if(result != null){
            TextView t = (TextView)this.findViewById(R.id.resultbox);
            Log.d("prabhu","result is ....."+result.getProperty(0).toString());
            t.setText("SOAP response:\n\n" + result.getProperty(0).toString());
        }

    }

这是我的wsdl:

http://localhost:8080/MyFirstWebService/services/FirstWebService?wsdl

     <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://sencide.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://sencide.com">
  <wsdl:documentation>Please Type your service description here</wsdl:documentation> 
- <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://sencide.com">
- <xs:element name="addTwoNumbers">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="firstNumber" type="xs:int" /> 
  <xs:element minOccurs="0" name="secondNumber" type="xs:int" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="addTwoNumbersResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="return" type="xs:int" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  </wsdl:types>
- <wsdl:message name="addTwoNumbersRequest">
  <wsdl:part name="parameters" element="ns:addTwoNumbers" /> 
  </wsdl:message>
- <wsdl:message name="addTwoNumbersResponse">
  <wsdl:part name="parameters" element="ns:addTwoNumbersResponse" /> 
  </wsdl:message>
- <wsdl:portType name="FirstWebServicePortType">
- <wsdl:operation name="addTwoNumbers">
  <wsdl:input message="ns:addTwoNumbersRequest" wsaw:Action="urn:addTwoNumbers" /> 
  <wsdl:output message="ns:addTwoNumbersResponse" wsaw:Action="urn:addTwoNumbersResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="FirstWebServiceSoap11Binding" type="ns:FirstWebServicePortType">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="addTwoNumbers">
  <soap:operation soapAction="urn:addTwoNumbers" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="FirstWebServiceSoap12Binding" type="ns:FirstWebServicePortType">
  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="addTwoNumbers">
  <soap12:operation soapAction="urn:addTwoNumbers" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap12:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="FirstWebServiceHttpBinding" type="ns:FirstWebServicePortType">
  <http:binding verb="POST" /> 
- <wsdl:operation name="addTwoNumbers">
  <http:operation location="addTwoNumbers" /> 
- <wsdl:input>
  <mime:content type="text/xml" part="parameters" /> 
  </wsdl:input>
- <wsdl:output>
  <mime:content type="text/xml" part="parameters" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="FirstWebService">
- <wsdl:port name="FirstWebServiceHttpSoap11Endpoint" binding="ns:FirstWebServiceSoap11Binding">
  <soap:address location="http://localhost:8080/MyFirstWebService/services/FirstWebService.FirstWebServiceHttpSoap11Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="FirstWebServiceHttpSoap12Endpoint" binding="ns:FirstWebServiceSoap12Binding">
  <soap12:address location="http://localhost:8080/MyFirstWebService/services/FirstWebService.FirstWebServiceHttpSoap12Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="FirstWebServiceHttpEndpoint" binding="ns:FirstWebServiceHttpBinding">
  <http:address location="http://localhost:8080/MyFirstWebService/services/FirstWebService.FirstWebServiceHttpEndpoint/" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

4 个答案:

答案 0 :(得分:2)

这可能是Android SDK /模拟器上DNS解析的问题。使用 ip-adress而不是主机名。

答案 1 :(得分:2)

请注意,在WSDL中,addTwoNumbers方法接受两个参数 - firstNumber和secondNumber。在你的代码中,你既没有设置。

request.addProperty("Parameter","Value");

......应该是......

request.addProperty("firstNumber","2"); // Insert favorite first number here. request.addProperty("secondNumber","2"); // Insert favorite second number here.

我发现有用的一件事就是在发生SOAP对话时查看它。有几种方法可以做到这一点。很多人打开了Transport的调试标志,并选择了SoapEnvelope的bodyIn和bodyOut成员变量。但对我来说最简单的事情是启动WireShark并查看XML和HTTP标头,因为它们是通过网络发送的。

我写了一篇你可以在这里找到的ksoap2-android教程......

http://www.shanekirk.com/2011/11/speaking-soap-with-android/

希望这会帮助你绕过它应该工作的方式。

答案 2 :(得分:0)

我得到了答案。 localhost donot在android中运行。我使用IP地址10.0.2.2而不是本地主机。谢谢你的帮助。

答案 3 :(得分:0)

根据您的WSDL,我建议您做一些修改:

private static String SOAP_ACTION = "http://com.sencide/FirstWebService";    
private static String NAMESPACE = "http://sencide.com";    
private static String METHOD_NAME = "addTwoNumbers";    
private static String URL = "http://localhost:8080/MyFirstWebService/services/FirstWebService?wsdl";

我希望能帮到你!!