从android调用网络服务

问题描述 投票:1回答:4

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

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>
android web-services axis
4个回答
2
投票

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


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成员var。但是对我来说,最简单的方法是启动WireShark,并查看通过有线发送的XML和HTTP标头。

我写了一个ksoap2-android教程,您可以在这里找到...

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

希望这可以帮助您将头枕固定在应该工作的方式上。


0
投票

我得到了答案:localhost在Android中不起作用。我使用IP地址10.0.2.2代替了本地主机。感谢您的帮助。


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";
© www.soinside.com 2019 - 2024. All rights reserved.