Calling a Java Web Service from Android via ksoap2
I have implemented a Java web service WelcomMsg and it is running on Apache Axis. The web service has only one method greetUs(). It returns a string "Greetings": When I call using the HttpTransport object, I get an exception and always get "false".
I guess the namespace is here. Please look into the code and tell me what is missing/incorrect. I have given the method on service, wsdl contents,and Android soap client contents.
The method in the service is as follows:
public class WelcomeMsg {
public String greetUs()
{
return "Greetings!!!";
}
}
Here is the wsdl:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:part element="impl:greetUsResponse" name="parameters">
</wsdl:part>
<wsdl:part element="impl:greetUs" name="parameters">
</wsdl:part>
<wsdl:operation name="greetUs">
<wsdl:input message="impl:greetUsRequest" name="greetUsRequest">
</wsdl:input>
<wsdl:output message="impl:greetUsResponse" name="greetUsResponse">
</wsdl:output>
</wsdl:operation>
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="greetUs">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="greetUsRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="greetUsResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:port binding="impl:WelcomeMsgSoapBinding" name="WelcomeMsg">
<wsdlsoap:address location="http://localhost:8080/NewWebService/services/WelcomeMsg"/>
</wsdl:port>
The Android Soap Client is as follows:
package com.example.HelloDroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.HttpTransportSE;
public class Droid extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "DefaultNamespace";
private static final String METHOD_NAME = "greetUs";
private sta开发者_开发问答tic final String NAMESPACE = "urn:DefaultNamespace";
private static final String URL = "http://192.168.186.136:12733/NewWebService/services/WelcomeMsg";
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.TextView01);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=false;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try{
ht.call(SOAP_ACTION, envelope);
//tv.setText("http set");
SoapPrimitive sp= (SoapPrimitive) envelope.getResponse();
tv.setText("Msg from service: "+sp);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Have you insertet the "uses-permission android:name="android.permission.INTERNET" to the AndroidManifest.xml ?
精彩评论