Developing Connected .NET Pocket PC Applications Using Web Services

.NET Compact Framework 1.0
 

Andreas Sjöström
businessanyplace.com

August 2003

Applies to:

   Microsoft® Windows® .NET Compact Framework
   Microsoft Windows Powered Pocket PC
   XML-based Web Services

Summary: Learn how to give users mobile access to the Internet by combining the connectivity of the Pocket PC and the dynamics of XML-based Web Services. (9 printed pages)

Download connected_PPC_apps.exe from the Microsoft Download Center.


Contents

Web Services Meets Mobility
Find Persons and Businesses by Phone Number
People Anyplace
Code Walk-through
Conclusion

Web Services Meets Mobility

Web Services is quickly finding its way into major system integration development efforts. In back office and legacy systems alike, Web Services has become a main focus in providing mechanisms to make data move more freely between and across long-closed boundaries. An entire industry has joined forces and pushes forward in overcoming both technical hurdles such as security and market specific hurdles such as the need of standardized message formats.

While these efforts continue, an entirely different market is rapidly maturing: the mobile devices, software and services market. Very interesting solution options arise in the cross section, where Web Services meets mobility. The Microsoft® Windows® .NET Compact Framework opens up the world of Web Services to Pocket PCs, and the wireless nature of Pocket PCs opens up the world of mobility to Web Services. This means that new fuel is added to the expansion of the XML-based Web Services provider market.

In this article, we will walk through the code behind a sample Pocket PC project that combines two Web Services from a company called ServiceObjects (http://www.serviceobjects.com/products/default.asp?bhcp=1).

Find Persons and Businesses by Phone Number

Dynamic On-Time Services (DOTS) is what the company ServiceObjects calls their commercially available Web Services. The sample Pocket PC application, "People Anyplace", combines DOTS GeoPhone (http://www.serviceobjects.com/products/dots_geophone.asp)and DOTS YellowPages (http://www.serviceobjects.com/products/dots_yellowpages.asp) to provide the user easy access to both address by phone number and hotels/restaurants nearby that address.

The DOTS GeoPhone XML Web Service provides access to more than 120 million U.S. residential telephone numbers linked with names and postal addresses. From a given phone number, the XML Web Service returns postal address, city, state, postal code, name, provider, and exchange location. The DOTS YellowPages XML Web Service opens up a business directory containing more than 14 million U.S. business listings and is searchable by keyword, category, name or popular category. A license is required from ServiceObjects when using these Web Services commercially. The license key "0" is valid for testing purposes only.

The scenario is that the Pocket PC user is traveling. People Anyplace provides accurate address information to phone numbers to persons, contacts, friends, businesses as well as provides information about nearby hotels and restaurants.

People Anyplace

The sample application consists of two forms. The main form allows the user to enter a phone number. The phone number is passed to the DOTS GeoPhone XML Web Service which returns the address, including the zip code, associated with the phone number.

Figure 1. XML Web Service returns address

When the user taps "Find Hotels & Restaurants," a new form is shown which lets the user search for restaurants and hotels within a given radius from the zip code of the address just looked up.

Figure 2. Nearby hotels and restaurants

The response populates a tree view control to increase usability. Now let's take a look at the code.

Code Walk-through

The first Web Service to be called is GeoPhone. This is how it's done:

      private void btnFindPerson_Click(object sender,
 System.EventArgs e)
      {
         // Instantiate Web Service
         GeoPhone geoPhone = new GeoPhone();         

         // Reset all controls to default
         resetControls();

         // Show wait cursor
         Cursor.Current = Cursors.WaitCursor;

         try
         {

            // Check that server is available over HTTP 
(port 80)
            if(CheckConnection.Once(geoPhone.Url))
            {
               // Get address
               Contact Person =
geoPhone.GetPhoneInfo(txtPhoneNumber.Text,"0").Contacts[0];
               // Display address
               txtName.Text = Person.Name;
               txtAddress.Text = Person.Address;
               txtCity.Text = Person.City;
               txtState.Text = Person.State;
               txtZip.Text = Person.Zip;

               this.btnYellowPages.Enabled = true;
            
            }
            else
            {
               // No connection!
               MessageBox.Show("Connection to Web Service
server could not be established!");
            }
         }

         catch (Exception ex)
         {
            // Disable Save As Contact button
            this.btnYellowPages.Enabled = false;

            MessageBox.Show("Could not make Web Service calls
(" + ex.Message + ")!");
         }
         finally
         {
            // Restore default cursor
            Cursor.Current = Cursors.Default;
         }
      
      }

It is important to first verify that the server is available, before the Web Service is called. While the user waits for the response, the cursor is set to an hourglass.

Once the response has been returned, the user can tap "Find Hotel & Restaurants" to find nearby hotels and restaurants. The YellowPages Web Service is called using the following code:

      private void btnFindBusiness_Click(object sender,
 System.EventArgs e)
      {
         DOTSYellowPages YP = new DOTSYellowPages();
         Listings HotelListings = new Listings();
         Listings RestaurantListings = new Listings();

         // Show wait cursor
         Cursor.Current = Cursors.WaitCursor;

         try
         {
            // Check that server is available over HTTP (port
 80)
            if(CheckConnection.Once(YP.Url))
            {
               // Get hotels
               HotelListings = YP.GetYPListingsByCategoryID(1279, YPZip, int.Parse(txtMiles.Text), "0");
   

               // Get restaurants
               RestaurantListings = YP.GetYPListingsByCategoryID(655, YPZip, int.Parse(txtMiles.Text), "0");
            
            
               // Clear treeview
               tvwBusiness.Nodes.Clear();

               // Call to fill the treeview with hotels
               FillTreeView(HotelListings);

               // Call to fill the treeview with 
restaurants
               FillTreeView(RestaurantListings);
            
            }
            else
            {
               // No connection!
               MessageBox.Show("Connection to Web Service 
server could not be established!");
            }
   
         }
         catch (Exception ex)
         {
            MessageBox.Show("Could not make Web Service calls 
(" + ex.Message + ")!");
         }
         finally
         {
            // Restore default cursor
            Cursor.Current = Cursors.Default;            
         }
      }

The YellowPages WSDL is found at: http://ws2.serviceobjects.net/yp/YellowPages.asmx?WSDL

Take a look at what is included in the Web Service response:

- <s:complexType name="Listing">
- <s:sequence>
  <s:element minOccurs="0" maxOccurs="1" name="Category" type="s:string" 
/> 
  <s:element minOccurs="0" maxOccurs="1" name="Description" 
type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="CompanyName" 
type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="Address" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="City" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="State" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="Zip" type="s:string" /> 
  <s:element minOccurs="0" maxOccurs="1" name="Phone" type="s:string" /> 
  <s:element minOccurs="1" maxOccurs="1" name="Latitude" type="s:decimal" 
/> 
  <s:element minOccurs="1" maxOccurs="1" name="Longitude" type="s:decimal" 
/> 
  </s:sequence>
  </s:complexType>

The People Anyplace application uses only a couple of the elements returned. Note that latitude and longitude is also returned. This means that we could add a cool killer feature that also pulled down a map of the neighborhood around the original address, and the hotel/restaurant! When GPS is integrated into mobile devices in the not so distant future, we will see location aspects added into the already-hot cross-section between Web Services and mobility. Isn't that cool?

The following code populates the tree view control:

      private void FillTreeView(Listings TreeListing)
      {
         TreeNode nod;

         try
         {
            tvwBusiness.BeginUpdate();
            foreach (Listing li in TreeListing.Listing)
            {
               nod = new TreeNode(li.CompanyName);         
               nod.Nodes.Add(li.Address);
               nod.Nodes.Add(li.Phone);
               tvwBusiness.Nodes.Add(nod);
            }
            tvwBusiness.EndUpdate();
         }

         catch (Exception ex)
         {
            MessageBox.Show("Could not parse results (" + 
ex.Message + ")!");
         }
         finally
         {
         tvwBusiness.Nodes.Clear();
         }
}

Conclusion

There are so many cool products and standards pushing the development in the same direction. New development tools, Internet connectivity, mobile devices and Web Services combined enables completely new software and services categories. The real Mobile Internet is finally here!

Show: