Wednesday, February 27, 2013

C# Web Services and Android – Part 1

C# web services are currently widely used to communicate with any other platforms (like mobile, web applications) through internet. Since last two decades usage of smart phone devices are really increased. When designing the web application we must consider about the smart devices fact as well. C# Web Services with Android tutorial series is really helpful to build communication pathway between C# web services and Android smart phone devices.  

User Story: There is a C# web application which can access through the PC and user needs Android based mobile application which provides selected functionalities of the web application. Web application provide functionality to retrieve full name of the person which belongs to the given user id. User needs Android based mobile application for previously mentioned web function. User uses his/her android device, starts the application then type user id and submits it. Android application should capable to communicate with web service and retrieve relevant data and display.

According to the previous user story we need to implement simple web service to retrieve the person details which belongs to the given id. Also need to implement Android based mobile application which can communicate with web service and display correct results.

How to create simple web service to retrieve person details by given id

In the web service we use business object called “Person”. Following code illustrate the properties of the Person object. We used serializable person object to communicate with mobile device the reason is only serializable objects can transfer through the network.


     /// 
     /// public class Person
     /// 
    [Serializable]
    public class Person
    {
        /// 
        /// Gets or sets the first name.
        /// 
        /// 
        /// The first name.
        /// 
        public string FirstName
        {
            get;
            set;
        }
        
        /// 
        /// Gets or sets the last name.
        /// 
        /// 
        /// The last name.
        /// 
        public string LastName
        {
            get;
            set;
        }

        /// 
        /// Gets or sets the id.
        /// 
        /// 
        /// The id.
        /// 
        public string Id
        {
            get;
            set;
        }
    }


There is another class we used to defined possible operations of person. Following class shows create sample data for person and retrieve relevant person details for given id (return suitable person object).


    /// 
    /// public class PersonOperations
    /// 
    public class PersonOperations
    {
        /// 
        /// Creates the person list.
        /// 
        /// person list
        public List CreatePersonList()
        {
            List personList = new List();

            Person person1 = new Person();

            person1.FirstName = "Iron";
            person1.LastName = "Man";
            person1.Id = "IM01";

            Person person2 = new Person();

            person2.FirstName = "Avatar";
            person2.LastName = "Aang";
            person2.Id = "AA02";

            Person person3 = new Person();

            person3.FirstName = "Avatar";
            person3.LastName = "Korra";
            person3.Id = "AA03";

            personList.Add(person1);
            personList.Add(person2);
            personList.Add(person3);

            return personList;
        }


        /// 
        /// Gets the person by id.
        /// 
        /// The user id.
        /// 
        /// relevant person
        /// 
        public Person GetPersonById(string userId)
        {
            List personList = this.CreatePersonList();

            if (string.IsNullOrEmpty(userId) || personList == null || personList.Count == 0)
            {
                return null;
            }

            Person result = personList.Where(x => x.Id.Equals(userId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

            if (result == null)
            {
                return null;
            }

            return result;
        }
    }


Following code snippets shows the simple web service. It simply passes the user submitted id to the function called “GetPersonById” and returns the relevant Person object.  In the web method it’s mandatory to define web service namespace. Because SOAP action use web service namespace to recognize the correct web service which hosted in the given host (URL).


    /// 
    /// Summary description for DemoService
    /// 
    [WebService(Namespace = "http://DemoWebService.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class DemoService : System.Web.Services.WebService
    {

        /// 
        /// Gets the person by id.
        /// 
        /// The user id.
        /// 
        /// relevant person
        /// 
        [WebMethod]
        public Person GetPersonById(string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                return null;
            }
            else
            {
                PersonOperations result = new PersonOperations();

                return result.GetPersonById(userId);
            }
        }
    }


Following screenshots shows the details of the web service which hosted in local host. How to host web service in local machine will be discuss in future.

Figure 1 - Web Service


This image demonstrates what are the web-methods define under the web service. Clicking on the function name (GetPersonById) user can trigger the web-method and make sure its functioning properly or not (see image – 2 and 3).

Figure 2 - Invoke the web service with suitable data


Following screen illustrates the result after invoking web-method with data. It shows what the properties of return object and values are.

Figure 3 - Results after invoking web-method with data


Following screen shows the description of the web service. That description contains all necessary details needs to invoke from outside (for example web service name space, soap action etc.).

Figure 4 - Description of the web service


Part two of this post will be post soon and it contains both Visual Studio C# solution and Eclipse project of Android mobile application.