27

I need to call a web service with a URL something like: "http://192.168.1.19/TestWeb/WebService.asmx" from android.

Please anyone help me with a full example?

Raghav Sood
  • 79,170
  • 20
  • 177
  • 186
Rajapandian
  • 6,699
  • 11
  • 35
  • 27
  • I don't understand. Your web service is in .NET. Where does Android come into it? Where is the HelloWorld method? – John Saunders Jun 26 '09 at 10:08
  • Thanks for the edit. What have you tried so far? What part are you having trouble with? – John Saunders Jun 26 '09 at 12:15
  • Why on earth did you start a new question instead of editing this one with more details? – blowdart Jun 27 '09 at 09:52
  • Pretty much the same question reposted here: http://stackoverflow.com/questions/1052300/how-to-call-a-net-webservice-from-android-using-ksoap2 – Paul Sasik Feb 16 '11 at 23:28
  • You can find an example [here](http://mypetprojects.blogspot.com/2009/05/communication-between-wcf-service-and.html). Hope it helps. – Niko Gamulin Jun 29 '09 at 07:38
  • Have a look at here http://www.anddev.org/web_services_-_an_xml-rpc_client_for_android-t646.html – rangalo Jun 26 '09 at 12:20
  • I have created the ASP.NET MVC webservice. i am struggling to call this webservice from the android application. Please help. – Karthick Jul 19 '13 at 06:54

1 Answers1

31

Finally, I got the solution for my own question.

Here is the code:

package projects.ksoap2sample;



import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.*;
import android.os.*;
import android.widget.TextView;

public class ksoap2sample extends Activity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";

    private static final String METHOD_NAME = "HelloWorld";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx";
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text1);
        call();

    }

    public void call()
    {
        try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            request.addProperty("passonString", "Rajapandian");

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();

            tv.setText(result.toString());
        } catch (Exception e) {
            tv.setText(e.getMessage());
            }
    }
}
Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
Rajapandian
  • 6,699
  • 11
  • 35
  • 27