You are on page 1of 8

MonoAndroid: Using dotnet webservice (ASMX)

By ThatsAlok, 10 Oct 2013


Sign Up to vote 5.00 (2 votes)
1

Download MonoAndroidWebService.zip - 11.4 KB Download MonoWebService Mobile App.zip - 15.4 KB

Introduction
Here, Once again I am back with another article in my series of MonoAndroid, for previous article refers at bottom portion. In this article I am going to demonstrate using ASMX Web Service (Non-WCF) in Mobile App. Web services are becoming integrated part of mobile and web applications. Mono Android support almost all type of Web Services like Restful, WCF and normal SOAP based web services . In this article I am going to create dotnet based web service and consuming same in my mobile application. In this article I will create Software Developer Hierarchy app. In this we will create dotnet web service, where we provide web method which will send the list of software person with their designations and show them on our android mobile ListView.

Step By Step we move forward


1. Create Android application by selecting New ->Solutions and provide its name M o n o W e b S e r v i c e

Figure 1: Creating Android Project! 2. Now by default, when you create project in the android, it contain a button. So application flow would be on click of button, we will invoke web service and fill the ListView with the retrieved data. So Button text contains Hello World and when we check button properties, it will show its referring to string resource, Means button label is coming from string table.

Now to change text on the button, you have to modify strings.xml underResources\values. Just look for name hello and change the name to Retrieve Data from Web service. See i marked line in bold.
Collapse | Copy Code

< ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < r e s o u r c e s > < s t r i n gn a m e = " h e l l o " > R e t r i e v eD a t a f r o mW e bs e r v i c e < / s t r i n g > < s t r i n gn a m e = " a p p _ n a m e " > M o n o W e b S e r v i c e < / s t r i n g > < / r e s o u r c e s >

3. Now add the ListView in the MainActivity file, after adding that, UI code looks something like this :-

Collapse | Copy Code

< ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < L i n e a r L a y o u t x m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i d " a n d r o i d : o r i e n t a t i o n = " v e r t i c a l " a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " f i l l _ p a r e n t " > < B u t t o n a n d r o i d : i d = " @ + i d / m y B u t t o n " a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " w r a p _ c o n t e n t " a n d r o i d : t e x t = " @ s t r i n g / h e l l o "/ > < L i s t V i e w a n d r o i d : m i n W i d t h = " 2 5 p x " a n d r o i d : m i n H e i g h t = " 2 5 p x " a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " m a t c h _ p a r e n t " a n d r o i d : i d = " @ + i d / M a i n _ L i s t V i e w "/ > < / L i n e a r L a y o u t >

4. Now time if for you to create web service in visual studio 2010. Now open the Create New Project, change .Net framework 3.5 to make ASP.NET Web Service Application projectavailable for selection.

Now create the project by name MonoAndroidWebService 5. Now change the name of default S e r v i c e 1 to M o n o D a t a S e r v i c e and delete anything else there. Now add G e t W e b S e r v i c e M o n o D a t a s ( ) method which returns hardcoded list of WebServiceMonoData
Collapse | Copy Code

p u b l i cc l a s sM o n o D a t a S e r v i c e:S y s t e m . W e b . S e r v i c e s . W e b S e r v i c e { [ W e b M e t h o d ] p u b l i c L i s t < W e b S e r v i c e M o n o D a t a >G e t W e b S e r v i c e M o n o D a t a s ( ) { v a rl s t F r a g m e n t D e m o=n e w L i s t < W e b S e r v i c e M o n o D a t a > { n e wW e b S e r v i c e M o n o D a t a ( ) { N a m e=" A l o kG u p t a " , D e s i g i n a t i o n=" S n rS o f t w a r eE n g i n e e r " } , n e wW e b S e r v i c e M o n o D a t a ( ) { N a m e=" J a s d e e p S i n g h " , D e s i g i n a t i o n=" S n rA u d i t o r( I n f o r m a t i o n ) " } , n e wW e b S e r v i c e M o n o D a t a ( ) { N a m e=" A s h i s hS r i v a s t a v a " , D e s i g i n a t i o n=" S o f t w a r eT e s tE n g i n e e r " } , n e w W e b S e r v i c e M o n o D a t a ( ) { N a m e=" H a r i h a r a nR a m c h a n d r a n " , D e s i g i n a t i o n=" P r o d u c tA r c h i t e c t " } } ; r e t u r nl s t F r a g m e n t D e m o ; } }

Now following is the code of WebServiceMonoData class


Collapse | Copy Code

[ S e r i a l i z a b l e ] p u b l i cc l a s sW e b S e r v i c e M o n o D a t a { p u b l i cs t r i n gD e s i g i n a t i o n{g e t ;s e t ;} }

p u b l i cs t r i n gN a m e{g e t ;s e t ;}

Since we are sending our class using services over http, we need to make our classSerializable . Build and compile webservice, so that it would be available for creating web reference. 6. Now In your mobile application, right click on the Project->Add-> Add Web Reference , see I marked that in following screenshot

Change web service URL to your local service address which ishttp://localhost:61847/MonoDataService.asmx in my case. Select framework .Net 2.0 web services and provide reference as MonoDataService

7. Now add following code in MainActivity file, I will explain the further down

Collapse | Copy Code

p u b l i cc l a s sM a i n A c t i v i t y:A c t i v i t y { p r o t e c t e do v e r r i d ev o i dO n C r e a t e( B u n d l e b u n d l e ) { b a s e . O n C r e a t e( b u n d l e ) ; / /S e to u rv i e wf r o mt h e" m a i n "l a y o u t r e s o u r c e S e t C o n t e n t V i e w( R e s o u r c e . L a y o u t . M a i n ) ; B u t t o nb u t t o n= F i n d V i e w B y I d < B u t t o n >( R e s o u r c e . I d . m y B u t t o n ) ; b u t t o n . C l i c k+ = O n W e b s e r v i c e R e t r i e v e d I n f o r m a t i o n ; } v o i dO n W e b s e r v i c e R e t r i e v e d I n f o r m a t i o n( o b j e c t s e n d e r ,E v e n t A r g se ) { v a ro b j M o n o A n d r o i d S e r v i c e=n e w M o n o D a t a S e r v i c e . M o n o D a t a S e r v i c e( ) ;o b j M o n o A n d r o i d S e r v i c e . B e g i n G e t W e b S e r v i c e M o n o D a t a s ( W e b S e r v i c e C a l l B a c k , o b j M o n o A n d r o i d S e r v i c e ) ; } v o i d W e b S e r v i c e C a l l B a c k( I A s y n c R e s u l ta r ) { v a ro b j M o n o A n d r o i d S e r v i c e= a r . A s y n c S t a t e a sM o n o D a t a S e r v i c e . M o n o D a t a S e r v i c e ; v a ra r r W e b S e r v i c e M o n o D a t a s = o b j M o n o A n d r o i d S e r v i c e . E n d G e t W e b S e r v i c e M o n o D a t a s( a r ) ; v a rl s t W e b S e r v i c e M o n o D a t a= n e wL i s t < M o n o W e b S e r v i c e . M o n o D a t a S e r v i c e . W e b S e r v i c e M o n o D a t a > ( a r r W e b S e r v i c e M o n o D a t a s ) ; v a rm a i n L i s t V i e w=F i n d V i e w B y I d < L i s t V i e w > ( R e s o u r c e . I d . M a i n _ L i s t V i e w ) ; m a i n L i s t V i e w . A d a p t e r=n e wL i s t B a s e A d a p t e r( t h i s , l s t W e b S e r v i c e M o n o D a t a ) ; } }

Above we are doing following On button click create the object of the MonoDataService.MonoDataService and invoke GetWebServiceMonoDatas method asynchronously. In the callback function, creating the List of WebServiceMonoData from retrieved array and passing it to our custom adapter to create ListView. Here is the code of our custom BaseAadapter derived class.
Collapse | Copy Code

p u b l i cc l a s sL i s t B a s e A d a p t e r :B a s e A d a p t e r < W e b S e r v i c e M o n o D a t a > { L i s t < M o n o W e b S e r v i c e . M o n o D a t a S e r v i c e . W e b S e r v i c e M o n o D a t a >_ l s t W e b S e r v i c e M o n o D a t a ; A c t i v i t y_ m a i n A c t i v i t y ; p u b l i cL i s t B a s e A d a p t e r( A c t i v i t ym a i n A c t i v i t y , L i s t < M o n o W e b S e r v i c e . M o n o D a t a S e r v i c e . W e b S e r v i c e M o n o D a t a >l s t W e b S e r v i c e M o n o D a t a ) { _ l s t W e b S e r v i c e M o n o D a t a=l s t W e b S e r v i c e M o n o D a t a ; _ m a i n A c t i v i t y=m a i n A c t i v i t y ; } # r e g i o ni m p l e m e n t e da b s t r a c tm e m b e r so fB a s e A d a p t e r p u b l i co v e r r i d el o n gG e t I t e m I d ( i n tp o s i t i o n ) {r e t u r np o s i t i o n ; } p u b l i co v e r r i d eV i e wG e t V i e w( i n tp o s i t i o n , V i e wc o n v e r t V i e w ,V i e w G r o u pp a r e n t ) {v a ri t e m=t h i s[ p o s i t i o n ] ; i f( c o n v e r t V i e w= = n u l l ) c o n v e r t V i e w=_ m a i n A c t i v i t y . L a y o u t I n f l a t e r . I n f l a t e ( A n d r o i d . R e s o u r c e . L a y o u t . S i m p l e L i s t I t e m 2 ,p a r e n t ,f a l s e ) ; c o n v e r t V i e w . F i n d V i e w B y I d < T e x t V i e w >( A n d r o i d . R e s o u r c e . I d . T e x t 1 ) . T e x t =i t e m . N a m e ; c o n v e r t V i e w . F i n d V i e w B y I d < T e x t V i e w >( A n d r o i d . R e s o u r c e . I d . T e x t 2 ) . T e x t = i t e m . D e s i g i n a t i o n ; r e t u r nc o n v e r t V i e w ; } p u b l i co v e r r i d ei n tC o u n t{g e t{ r e t u r n_ l s t W e b S e r v i c e M o n o D a t a = =n u l l ?1 :_ l s t W e b S e r v i c e M o n o D a t a . C o u n t ; } } # e n d r e g i o n # r e g i o ni m p l e m e n t e da b s t r a c tm e m b e r so fB a s e A d a p t e r p u b l i co v e r r i d e W e b S e r v i c e M o n o D a t at h i s[ i n ti n d e x ]{ g e t{r e t u r n_ l s t W e b S e r v i c e M o n o D a t a = =n u l l ?n u l l : _ l s t W e b S e r v i c e M o n o D a t a [ i n d e x ] ; } } # e n d r e g i o n }

You can learn more about creating custom Base Adapter here 8. Now build and run your mobile application, you will see this ugly exception popping up :-

Problem here is that your mobile app trying to find the Web Service in local mobile, where its not present as it present on the development machine. Now to access web service from your emulator you need to use magic IP address 10.0.2.2, instead of LocalHost. Now while creating web service object pass the URL like this :Collapse | Copy Code

v o i dO n W e b s e r v i c e R e t r i e v e d I n f o r m a t i o n( o b j e c ts e n d e r ,E v e n t A r g se ) { v a r o b j M o n o A n d r o i d S e r v i c e= n e wM o n o D a t a S e r v i c e . M o n o D a t a S e r v i c e ( " h t t p : / / 1 0 . 0 . 2 . 2 : 6 1 8 4 7 / M o n o D a t a S e r v i c e . a s m x " ) ; o b j M o n o A n d r o i d S e r v i c e . B e g i n G e t W e b S e r v i c e M o n o D a t a s( W e b S e r v i c e C a l l B a c k , o b j M o n o A n d r o i d S e r v i c e ) ; }

9. Try again to build and run mobile app, you again encountered exception, this time its due to updating UI control from worker thread.

Now to solve this problem, move these two line under R u n O n U i T h r e a d underWebServiceCallBack method like this :Collapse | Copy Code

R u n O n U i T h r e a d( ( )= >{ v a rm a i n L i s t V i e w=F i n d V i e w B y I d < L i s t V i e w > ( R e s o u r c e . I d . M a i n _ L i s t V i e w ) ; m a i n L i s t V i e w . A d a p t e r=n e wL i s t B a s e A d a p t e r( t h i s , l s t W e b S e r v i c e M o n o D a t a ) ; } ) ;

10. Now Build and Run the application, On 4 Inch Screen On Button Click

Magic IP table
This section text is taken from here, word by word. The virtual router for each instance manages the 10.0.2/24 network address space all addresses managed by the router are in the form of 10.0.2.<xx>, where <xx> is a number. Addresses within this space are pre-allocated by the emulator/router as follows: Network Description

Address 10.0.2.1 10.0.2.2 10.0.2.3 10.0.2.4 / 10.0.2.5 / 10.0.2.6 10.0.2.15 127.0.0.1 Router/gateway address Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine) First DNS server Optional second, third and fourth DNS server (if any) The emulated device's own network/ethernet interface The emulated device's own loopback interface

Points of Interest
I have used MonoAndroid for C# and Xamarin Studio to build this tutorial. Watch out, if I continue learning it, you can expect more articles coming soon. Though Xamarin Studio is proprietary software, however they provide free starter version to built, test and publish android application. I am using same for my learning.

Articles in this series!


MonoAndroid: Writing custom generic BaseAdapter in C# MonoAndroid: Using GridView in your mobile application MonoAndroid: Using TabHost in your mobile applications MonoAndroid: Using Fragments in mobile app MonoAndroid: Using Started Service MonoAndroid: Calling secondary activity[^]

Tips/Tricks in this Series


MonoAndroid: Android Notification service MonoAndroid: Using bound Services

History
22-Aug-2013: First Version 06-Sept-2013: Updated Tips and Tricks Section 10-Oct-2013: Updated Tips and Tricks Section

License
This article, along with any associated source code and files, is licensed under The Code Project Open

License (CPOL)

About the Author


ThatsAlok
Software Developer India

He used to have biography here writes his biography on his behalf

, but now he will hire someone (for free offcourse ?? ), Who

He is Great Fan of Mr. Johan Rosengren (his idol),Lim Bio Liong, Nishant S and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!

In October 2005, He joined the League of Visual C++ MVP