CONTENT
 PRODUCTS
 ARTICLES
 DOWNLOAD
 SOURCE CODE
 BOOK
 ABOUT
 HOME

MVP

ADS




Chart Shipping Rates with .NET and Pocket PC Phone Edition
Take advantage of the native Internet connectivity of Pocket PC Phone Edition and the native support for XML Web Services in the .NET tools to create an application that compares shipping rates and present the result in a chart.

The Web application hype taught us the benefit with online interactivity and up-to-date information. But the downside was the poor user interface and application navigation. With Pocket PC Phone Edition and XML Web Services, there is now an alternative to create applications with a rich Windows user interface while taking advantage of the benefits that online data bring. The creation of such applications have been made significantly easier with the new Smart Device Extensions and .NET Compact Framework.

Get the sample source code!
Shipping Anyplace
Shipping Anyplace

Comparing Shipping Rates
A lot of packages are sent all over the world every day and there are many shipping options available. On many markets, there are price comparison services available. What if there was a way to easily compare the shipping rates? Such an application needs to be dynamic to allow weight, origin, and destination to be entered. Much like the rate calculators provided by most shipping companies, but it would be great if the comparison could be made in one place and easily understandable. Wouldn't a chart be nice?

What if the functionality of that application was already available on the Internet? The increasing number of Web Services makes more and more functionality available. Great places to find Web Services are Xmethods.net and salcentral.

Shipping Rates Comparison Web Service
The first Web Service needed is one created by ServiceObjects called ShippingComparison. This Web Service compares the rates from various shipping companies. It takes two addresses and the weight of the package as input and provides a number of shipping rates for each company.

Charting Web Service
To make the charting happen, the Web Service by GeneXus called GXChart is used. This Web Service takes a pointer to the data (as an URL), a number of chart attributes, and returns a chart image.

Let's see what can be done with these two Web Services on a Pocket PC Phone Edition!

Shipping Anyplace Sample
The Shipping Anyplace application is an application built with SDE (Smart Device Extensions) for .NET CF (Compact Framework) that combines the shipping rates Web Service (see above) and the charting Web Service (see above). The application consists of one form:

Shipping Anyplace sample
Shipping Anyplace sample

When the application is started, it allows for shipping information input. When the weight, origin, and target location is entered, a tap on the Get Rates button will get the shipping rates from a number of companies. First, a call is made to the shipping rate Web Service to find out the rates for the specified shipping information (weight, origin, and destination), and then a virtual XML file is generated from that data. The XML file is fed into the call to generate a chart image. Finally, the chart image is retrieved and shown in the bottom half of the screen.

The goal of this sample is to make a comparison for the delivery of a package the next day (regardless of arrival time that day), and therefore only one rate per shipping company have been used.

Code Walkthrough
To access the Web Services, two Web references needs to be added from Visual Studio .NET. To simplify the use of the Web Services, the following code was added:
  using Shipping.net.serviceobjects.ws;
  using Shipping.com.gxchart.www;
All the code that actually does something is located in the Click event of the Get Rates button (see figure above). This is the declarations:
  ShippingComparison shipRates = new ShippingComparison();
  Rates rates = new Rates();
  WebChart webChart = new WebChart();
  string url = "";
The shipRates object is an instance of the shipping rates Web Service. The method used (GetShippingRates, see below) returns a complex type called "Rates" and therefore an object (rates) to store the return value is needed. Note that the Web Service defines the type (Rates). The webChart object is an instance of the charting Web Service. The next line of code simply shows the wait cursor:
  WaitCursor.Show(true);
The WaitCursor class is a reusable component that shows how to make Windows API calls from .NET CF and also why static methods (like Show) are truly useful. The first Web Service call to get the shipping rates looks like this:
  try
  {
    // Get shipping rates
    rates = shipRates.GetShippingRates(int.Parse(txtWeight.Text),
            txtFromCity.Text,txtFromState.Text,txtFromZip.Text,"US",
            txtToCity.Text,txtToState.Text,txtToZip.Text,"US",15,"0");
The parameter values are retrieved from the TextBoxes and the first is converted to an integer type. Note that "US" is hard-coded as country for both origin and destination. "15" is a mask for which services to query (see web service definition for details) and the final "0" is a dummy license code. Next, an URL is created to generate the XML data used as input for the charting Web Service:
  // Create URL for XML data
  url = "http://www.businessanyplace.net/sample/gxchartdata.asp?";
  foreach(Rate rate in rates.Rate)
  {
    switch(rate.Shipper)
    {
      case "USPS":
        if(rate.Service == "EXPRESS")
          url += "USPS=" + rate.Postage;
        break;
      case "UPS":
        if(rate.Service == "13")
          url += "&UPS=" + rate.Postage;
        break;
      case "FedEx":
        if(rate.Service == "Standard Overnight")
          url += "&FedEx=" + rate.Postage;
        break;
      case "DHL":
        if(rate.Service == "WorldWide Express")
          url += "&DHL=" + rate.Postage;
        break;
    }
  }
The URL points to an ASP page that transforms the passed parameters into a virtual XML file that can be used as the data source for the charting Web Service. To see what the XML file looks like, here is a sample. Now it is time to make the call to the charting Web Service:
  // Get image URL
  url = webChart.MakeChart(url, 240, 184, "Column", 20, 20, "",
                           "None","", "", "", "", 0, "");
The return value is another URL. This time the URL points to an image file with the generated chart. The first parameter is the data source (the virtual XML file), the next two are the dimensions followed by the type of chart desired. The charting Web Service offers no less than 32 different chart types. The "None" parameter indicates that no legend should be included in the chart. Now the image needs to be loaded into the Picture control:
  // Get HTTP stream from image URL
  HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
  HttpWebResponse resp = (HttpWebResponse) req.GetResponse();

  // Write stream to file
  FileStream file = new FileStream(@"\Windows\shipchart.gif", FileMode.Create);
  BinaryWriter fileStream = new BinaryWriter(file);
  BinaryReader picStream = new BinaryReader(resp.GetResponseStream());
  try
  {
    while(true)
      fileStream.Write(picStream.ReadByte());
  }
  catch { }
  fileStream.Close();

  // Set picture from file
  System.Drawing.Bitmap gif = new
    System.Drawing.Bitmap(@"\Windows\shipchart.gif");
  picChart.Image = gif;
This might look a bit complicated, but as the Picture control (in the beta 1 of the .NET CF) does not load an image from a stream, the image need to be saved to a file first. The file can then be loaded into the Picture control.
  }
  catch
  {
    MessageBox.Show("Could not make Web Service calls!");
  }
  finally
  {
    WaitCursor.Show(false);
  }
The last piece of code simply takes care of any problems with calling the Web Services and also makes sure the wait cursor is removed.

For a complete example, please download this article's sample source code.

Conclusion
The combination of Web Services can be a very effective way of increasing the value delivered by mobile applications. Also, the combination of the Pocket PC Phone Edition, with its native connectivity, and the new tools (VS.NET, SDE, and .NET CF), with their native support for web services, eases the creation of those mobile applications. Find out what Web Services could create value for you, and get started!

Any comments?


©2001-2009 Christian Forsberg & Andreas Sjöström