Tuesday, November 16, 2010

How to call web service in windows mobile

I have observed so many times on Microsoft and DNS (Dot Net Spider) forum in smart device category there is asking about web service call in windows mobile. So I have answered there and I have decided to write explained article about web service all in windows mobile application using C#.NET. This application is also useful for the US’s people only to get the one week weather forecast temperature.
In this article, I have considered two live web services used for Weather Forecast and Geo IP Service. The weather forecast web service provide one week weather forecast for place name in USA only. And Geo IP Service enables us to easily look up countries by IP address.
The WebserviceX.NET provides the free the web service provide access to functionality and data via web services
http://www.webservicex.net/ws/default.aspx
Let us create a project to consume web service in windows mobile application.
Step-1: Create smart device project using Visual Studio 2008.


Enter the project name and choose the project path location and press OK button.
Step-2: Select the Target platform and select .NET Compact framework and press OK button.
Step-3: Now let add the web service references in the project. Go to solution explorer of the project and right click on the References then a context menu will appear and click on the Add Web References…
Step-4: After click on the Add Web References… option then Add Web References dialog window will appear in the URL textbox I have entered “Weather Forecast” web service URL and click on Go Button right side of the textbox.
http://www.webservicex.net/WeatherForecast.asmx
Then web service page is displaying with the web methods, it mean web service is ready to use in our application, now enter the name of the web service which is using in the application here I have entered “WebSerWeatherForecast” and click on the Add Reference Button. See below picture.
Step-5: For add the Geo IP Web Service. Follow the Step-1 and Step-4
http://www.webservicex.net/geoipservice.asmx
Step-6: Now designing the UI for displaying data for Weather Forecast and Geo IP service acceding to requirement. Once the UI is ready then we can go ahead with the coding part to invoke the web service and download image for weather forecasting and GEO IP data. See below code snippet.
using WebServiceCall.WebSerGepIP;
using WebServiceCall.WebSerWeatherForecast;
using System.Net;
private void mnWeather_Click(object sender, EventArgs e)
{
using (frmCity ofrmCity = new frmCity())
{
ofrmCity.ShowDialog();
}
Cursor.Current = Cursors.WaitCursor;
WeatherForecast oWeatherForecast = null;
try
{
oWeatherForecast = new WeatherForecast();
object o = oWeatherForecast.GetWeatherByPlaceName(Program.strSelectedCity);
InitTextValues(o);
panel1.Visible = true;
panel2.Visible = true;
Title.Visible = false;
Message.Visible = false;
}
catch (WebException wx)
{
Cursor.Current = Cursors.Default;
switch (wx.Status)
{
case WebExceptionStatus.ConnectFailure:
MessageBox.Show(wx.Message);
break;
}
}
catch (Exception ex)
{
Cursor.Current = Cursors.Default;
MessageBox.Show(ex.Message);
}
Cursor.Current = Cursors.Default;
}
The above code follows the sequence of code to choose the City first for getting the weather forecast information. Then create an instance of the web service of the weather forecasting; and invoked the method GetWeatherByPlaceName() this method has one string type of parameter which is the city name and this method will return an object which has all the weather forecasting data and pass the returned object to the InitTextValues() method which will parsing the object data.
Step-7: Now can go ahead for the Geo IP data code see below code snippet.
private void mnGeoIP_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
GeoIP oData = null;
try
{
GeoIPService oWebSerGepIP = new GeoIPService();
oData = oWebSerGepIP.GetGeoIPContext();
Cursor.Current = Cursors.Default;
using (GeoAreaIP ofrmCity = new GeoAreaIP(oData))
{
ofrmCity.ShowDialog();
}
}
catch (WebException wx)
{
Cursor.Current = Cursors.Default;
switch (wx.Status)
{
case WebExceptionStatus.ConnectFailure:
MessageBox.Show(wx.Message);
break;
}
}
catch (Exception ex)
{
Cursor.Current = Cursors.Default;
MessageBox.Show(ex.Message);
}
}
The above code snippet is responsible to invoke the GeoIPService. Here I have create an object of the GeoIPService its instance allows us to invoke the GetGeoIPContext() method and its return type is GeoIP which will hold the data Geo IP Data.
Step-8: Now our application is ready to get web service data. In this case make sure that we need to cradle the Windows Mobile emulator see below picture to connect the emulator or device. Click on connect to device button.
After click on connect to device button below window will appear to see the device state is connected successful.
Now go to Tool Menu and click on it and click on the “Device Emulator Manager…” from the pool down menu see below picture.
After click on the Device Emulator Manager… a Device Emulator Manager window will open see below picture. It is showing green arrow on connected emulator or device.
Now right click on it and click on the Cradle from the context menu as depicted in the below picture.
After click on Cradle then Windows Mobile device center will connecting to Emulator this allows the internet access in the emulator.
Note if you are using Windows 7 and Vista Os then you have to install Windows Mobile Device center and if you are using Windows XP then you have to use Active Sync 4.5 for the same.
Now click on the “Connect Without setting up your device” after click on it the WMDC will connected properly see below picture.
Now run the application to press F5 Button the application’s first screen will appear see below picture

Now click on the Web Service Menu see below picture.




Now click on the Weather forecast option from the menu then it will open a window to select a city to get the weather forecast information of it. See below picture.


Now select the city from the drop down list and press Save Button then the web service is invoking and returns the appropriate data according to city see below screen.

Now click on the details button to view the one week weather forecast temperature.



Now go to main menu and click on the Web Service Menu -> Get Your Geo IP Area then it will open a screen with Geo IP Data. See below screen.
This application is also useful for the US’s people only to get the one week weather forecast temperature.
Download Source code here

Thanks you for your time.