Accessing Webservices using SOAP Example - ANDROID - Helper

Saturday, April 21, 2012

Accessing Webservices using SOAP Example


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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Celsius to Farenheit" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width=" fill_parent"
        android:layout_height="wrap_content"
        android:numeric="integer"
        android:singleLine="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Convert to Farenheit" />


    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

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) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);

                        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();
                        celsiusPI.setName("Celsius");
                        celsiusPI.setValue(celsius);
                        celsiusPI.setType(double.class);
                        request.addProperty(celsiusPI);

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                                SoapEnvelope.VER11);
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(request);
                        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) {
                                    e.printStackTrace();
                        }

                        return null;
            }
}

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

The output will be



25 comments:

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

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

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

    ReplyDelete
  4. it gives my this error

    the application has stopped unexpected.please try again .

    ReplyDelete
  5. Thanks.This code works fine.nice work

    ReplyDelete
  6. 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.

    ReplyDelete
  7. return null; is not required.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. the application has stopped unexpected.please try again

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

    ReplyDelete
  11. 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

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

    ReplyDelete
  13. 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.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This code is very help for me..........
    Thanks.....

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

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

    appsocio

    ReplyDelete
  18. i have got the many ideas form your post. keep posting.

    ReplyDelete
  19. Thanks for sharing the great blog post with us! We provide custom Uber for trucking app development solution and truck dispatch software for logistics and freight businesses to bring simplicity and efficiency to their dispatch operations.
    Hire Angularjs Developers | Truck Dispatch Software | Taxi App Development

    ReplyDelete