• Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 23 comments

Android – Accessing Webservices using SOAP Example

Source main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:text="@string/hello" />

        android:text="Celsius to Farenheit" />

        android:layout_width=" fill_parent"
        android:singleLine="true" />

        android:text="Convert to Farenheit" />

        android:text="" />


Source WebserviceActivity.java

package com.webserviceexample;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WebserviceActivity extends Activity {

            private final String NAMESPACE = "http://tempuri.org/";
            private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
            private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
            private final String METHOD_NAME = "CelsiusToFahrenheit";

            Button b;

            TextView tv;

            EditText et;

            public void onCreate(Bundle savedInstanceState) {

                        et = (EditText) findViewById(R.id.editText1);

                        tv = (TextView) findViewById(R.id.tv_result);

                        b = (Button) findViewById(R.id.button1);

                        b.setOnClickListener(new OnClickListener() {
                                    public void onClick(View v) {

                                                String result = getFahrenheit(et.getText().toString());

                                                tv.setText(result+"° F");


            public String getFahrenheit(String celsius) {

                        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                        PropertyInfo celsiusPI = new PropertyInfo();

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        envelope.dotNet = true;
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                        try {
                                    androidHttpTransport.call(SOAP_ACTION, envelope);
                                    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                                    Log.i("Webservice Output", response.toString());

                                    return response.toString();

                        } catch (Exception e) {

                        return null;

·         Add Internet Permission in your AndroidManifest.xml file.
·         Download ksoap2.jar and add to buildpath.

The output will be

23 Responses so far.

  1. pavan says:

    i am using this code. but i am getting output as null0 F

  2. i am using your code. but i am getting output as null0 F

  3. nice blog ! i think all having prob because of webservice

  4. محمد says:

    it gives my this error

    the application has stopped unexpected.please try again .

  5. Anonymous says:

    Thanks.This code works fine.nice work

  6. Anonymous says:

    hi, can you suggest me which is the best json or soap ? main thing is security and code efficiency . because up to now i am just using only json. so if soap is better then json then i will use that. but i think its too lengthy for code developing compare to json.

  7. ragu says:

    return null; is not required.

  8. ragu says:
    This comment has been removed by the author.
  9. ABHI says:

    the application has stopped unexpected.please try again

  10. Not working i get only null value

  11. web hosting Pakistan
    As an entrepreneur, you only think about your business venture and leave everything to us.

  12. Its a great example given by you that is provide the logic of how android access the web services using the SOAP.Through this blog i have got the ideas to implement the android apps...Thanks

    Android App Development

  13. I feel pleasure to read the content that you are posting.
    click over here now

  14. Anonymous says:

    Thanks for sharing apk Games this great list. There is no doubt it’s important to do a natural link building without spamming to gain better places in Google.

  15. Unknown says:
    This comment has been removed by the author.
  16. This code is very help for me..........

  17. Excellent article you have shared thanks for sharing. I appreciated of you.iPhone Apps Development Company

  18. Good work by the blog writer! such a nice and informative article keep sharing more articles.


  19. Mobisoft Infotech offers comprehensive and custom-built mobility solutions and services to startups and enterprises by leveraging our years of experience shipping high-quality digital products. By adopting the best industry standards, processes, technologies, and tools for custom software development, we are able to generate much higher ROI for our customers, making us one of the top mobile app development companies in Pune.

    android app development company

Leave a Reply