【问题标题】:Call web service from android从 android 调用 web 服务
【发布时间】:2012-01-04 12:03:21
【问题描述】:

我使用 this tutorial 创建了一个 Web 服务。我已成功创建 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>

【问题讨论】:

  • Now I want call this web service from android. I am trying since two days without any result.... 您的代码在哪里?... 向我们展示您到目前为止所做的尝试。如果您的应用程序被强制关闭/异常,那么也发布 logcat
  • 您尝试了哪些方法,哪些方法不起作用?你有例外吗?
  • 您确实需要编辑您的问题以提供更多详细信息。以目前的形式无法回答。

标签: android web-services axis


【解决方案1】:

这可能是 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 成员变量。但对我来说最简单的事情是启动 WireShark 并查看通过网络发送的 XML 和 HTTP 标头。

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

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

希望这能帮助您了解它的工作方式。

【讨论】:

  • 嗨,现在我已经完成了参数。现在结果也为空。你能告诉我使用 WSDL 在我的代码中字符串 SOAP_ACTION,NAMESPACE,METHOD_NAME,URL 的值吗?
【解决方案3】:

我得到了答案:localhost 在 Android 中不起作用。我使用了 IP 地址 10.0.2.2 而不是 localhost。感谢您的帮助。

【讨论】:

  • Prabhu M:你应该接受给出这个想法的答案。不要将你的建议标记为已接受的答案。
  • @karthik:我相信你上面给出的建议是错误的。如果愿意,人们可以将自己的答案标记为解决方案。将其他人的答案标记为解决方案当然是一种善意,但这不是强制性的。
【解决方案4】:

根据您的 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";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 2011-12-06
    • 2011-08-28
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    相关资源
    最近更新 更多