Using Web Services to Build Your Own Weather Application

Web services allow different applications from different sources to communicate with each other without time-consuming custom coding. All communication is in XML, and they are not tied to any operating system or programming language.

This post will explain how to consume a third party web service in a Mono for Android application to build your own simple weather application.

ABOUT CDYNE WEATHER

CDYNE is a website with many free and commercial web services. CDYNE Weather is a free web service that provides weather information in the United States by zipcode. This information is derived from the National Oceanic and Atmospheric Administration’s weather service into a clean and easy to parse XML format. In this post, we will only use the GetCityWeatherByZIP method in this web service which yields the weather information for a specific zipcode.

The complete service URL for the CDYNE Weather Web Service is:

http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl

If this link wouldn’t work for some reason, consult the CDYNE wiki site.

CREATE NEW PROJECT

Create a new project in your Visual Studio by using the Mono for Android Application project template. This step is the same as the one mentioned in the “Hello World” Application post.

ADDING WEB REFERENCE

You can add a Web Reference in Mono for Android like any other .NET applications by going to Project > Add Web Reference:

Enter the web service URL for CDYNE Weather that was provided above, and then click on the Add Reference button to import the service into the project:

Make sure that your new Solution Explorer should like this:

DESIGN and CODING

Layout Design

First, we will modify the layout resource file (Main.axml) to place an EditText (TextBox) to enter a zipcode, a Button to attach the web service call(s), and a TextView (Label) to display the weather information.

Notice that there is an android:numeric attribute in txtZip which is set to integer. This is because we only want numbers in the zipcode field.

This XML based constraints feature of Android (and Mono for Android as well) is quite powerful and useful. You should explore all possible attributes that go with a particular element to extract maximum benefits from this shorthand way of setting constraints.

Coding for Activity File

We will name our application as “Weatherdroyd”  declaratively in the custom attribute, and the OnCreate method will be as in the following:

The code with its comments is straightforward and doesn’t need any special explanation.

RUN via EMULATOR

Launch the emulator and deploy your application to your selected virtual device:

Note: How to accomplish this step was explained before in the “Launch Emulator and Debug” section of the “Hello World” Application post.

POSSIBLE ENHANCEMENTS

You can visit the Using WebView Class to Build Your Own Simple Twitter App post, if you would like to:

- change the application icon to look more professional, or
remove the title bar to provide more space.

You may also want to create options menu to place Reset and Close buttons, or set the screen orientation to landscape in your application.

CONCLUSION

This post demonstrated how to import a third party web service and consume it in a Mono for Android application. The tricky part is making the correct calls to the web service and binding the return values to the user interface elements defined in the layout resource file.

Leave a Comment

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: