1

I have created a web service using this tutorial . I have successfully created web service client . Its working fine for me. Now I want call this web service from android. I am trying since two days without any result. Please help me to fix this.

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());
        }

    }

This is my 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>
Prabhu M
  • 3,425
  • 8
  • 43
  • 81
  • 1
    `Now I want call this web service from android. I am trying since two days without any result.`... Where is your code ?... Show us what you have tried so far. If your app gets force closed/exception then post the logcat as well – Kartik Domadiya Nov 23 '11 at 06:24
  • what have you tried and what exactly is not working? Are you getting an exception? – Umesh Nov 23 '11 at 06:24
  • 1
    possible duplicate of http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-with-android – Gryphius Nov 23 '11 at 06:27
  • You really need to edit your question to provide more detail. This is impossible to answer in its current form. – Tim Post Nov 23 '11 at 06:37

4 Answers4

2

This may be a problem with DNS resolution on the Android SDK/emulator. Use the ip-adress instead of the hostname.

hasanghaforian
  • 13,142
  • 8
  • 71
  • 144
2

Note in your WSDL that the addTwoNumbers method accepts two parameters - firstNumber and secondNumber. In your code, you're setting neither.

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

...should probably be...

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

One thing I've found helpful is to look at the SOAP conversation as it happens. There are a couple of ways you can do this. A lot of people turn on Transport's debug flag and pick apart the SoapEnvelope's bodyIn and bodyOut member vars. But the easiest thing for me is to fire up WireShark and look at XML and HTTP headers as they're sent across the wire.

I've written a ksoap2-android tutorial you can find here...

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

Hopefully that'll help you wrap your head around the way it's supposed to work.

Shane Kirk
  • 281
  • 2
  • 7
  • Hi, Now I have done with the parameters.Now also result is null. Can u please tell me the values for strings SOAP_ACTION,NAMESPACE,METHOD_NAME ,URL in my code by using WSDL. – Prabhu M Nov 24 '11 at 04:15
0

According your WSDL I suggest you do some modifications :

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";
halfer
  • 18,701
  • 13
  • 79
  • 158
Shessuky
  • 1,418
  • 15
  • 22
0

I got the answer: localhost does not work in Android. I used the IP address 10.0.2.2 instead of localhost. Thanks for the help.

halfer
  • 18,701
  • 13
  • 79
  • 158
Prabhu M
  • 3,425
  • 8
  • 43
  • 81
  • 1
    Prabhu M:You should accept the answer who gave the idea.Don't mark your suggestion as accepted answer. – karthik May 22 '12 at 12:54
  • @karthik: the advice you have given above is, I believe, wrong. People may mark their own answers as the solution if they wish. It is certainly a kindness to mark other people's answers as the solution instead, but it is not mandatory. – halfer Feb 08 '20 at 15:16