MonoDroid – Consume a Web Service in an Android application with C#

In this application I will describe how to consume a web service in your MonoDroid application using VS 2010.
To install MonoDroid navigate to http://mono-android.net/Installation. Follow the instructions and download all the necessary files.
Created a new MonoDroid application named monoAndroidapplication.
Now Go to Project -> Add Web Reference. Enter in the URL of your web service and click Add Reference I will consume a webservice (http://soatest.parasoft.com/calculator.wsdl) which will add 2 values and will display. In this webservice you can also perform division and multiplication.
Alternatively you can use your own web service, a simple one that can perform addition of 2 values provided with parameters

Now In the Activity.cs file you have to write the code below.

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace MonoAndroidApplicationListView
{
    [Activity(Label = “MonoAndroidApplicationListView”, MainLauncher = true)]
    public class Activity1 : Activity
    {
        int count = 1;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the “main” layout resource
            SetContentView(Resource.Layout.Main);
            // Get our button from the layout resource,
            // and attach an event to it
            // Create a new Button and set it as our view  
            //Button button = FindViewById<Button>(Resource.Id.MyButton);
            Button button = new Button(this);   
            button.Text = “Consume!”;
            button.Click += button_Click;
            SetContentView(button);  
            //button.Click += delegate { button.Text = string.Format(“{0} clicks!”, count++); };
        }
        private void button_Click(object sender, System.EventArgs e)
        {
            // Create our web service and use it to add 2 numbers   
            var ws = new Calculator();
            var result = ws.add(25, 10);
            // Show the result in a toast   
            Toast.MakeText(this, result.ToString(), ToastLength.Long).Show();
        }  
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s