SIGN UP MEMBER LOGIN:    
ARTICLE

Consume a Web Service in an Android application with C# MonoAnDroid application

Posted by Shirsendu Nandi Articles | Android Programming March 15, 2011
In this application I will describe how to consume a web service in to your MonoDroid application using vs 2010. (MonoDroid will not work in VS 2010 Express edition).
Reader Level:

Description:

In this application I will describe how to consume a web service in your MonoDroid application using VS 2010. (MonoDroid will not work in VS 2010 Express edition).

To install MonoDroid navigate to http://mono-android.net/Installation

Follow the instructions and download all the necessary files.

Figure 1.

webservice1.gif

In the Figure 1. I have created a new MonoDroid application named monoAndroidapplication.

webservice2.gif

Now Like Figure 2 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.

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();
        }  

    }
}

Now in the figure below you will see the web service marked in red.

webservice3.gif

Run the Code by pressing F5 and you will see the result.

webservice4.gif

Note: Web services take time to access. Consider running them in a background thread. So be patient.

Login to add your contents and source code to this article
share this article :
post comment
 

thxx mahesh for your inspiration.

Posted by Shirsendu Nandi Mar 17, 2011

OK thanks!!!

Posted by Karthikeyan Anbarasan Mar 16, 2011

Good work! Nice to see more and more guys building Mono apps.

Posted by Mahesh Chand Mar 16, 2011

No it is an opensource.You only need vs 2010 license premium or ultimate edition.This mono android will not run on vs 2010 express edition

Posted by Shirsendu Nandi Mar 16, 2011

Do this framework is licensed to use in development?

Posted by Karthikeyan Anbarasan Mar 15, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor