Mining Google Web Services: Building Applications with the Google API

The Pocket PC is the most capable of the mobile devices you can use for a Google Web Services application. You can use local storage with this device and perform a multitude of tasks that some mobile devices can't, such as limited data analysis using the copy of Pocket Excel provided with the operating system. This platform even helps you create reports using Pocket Word. In sum, you can use a Pocket PC for most Google Web Services tasks ”at least in a limited way.

Tip  

You may have to search the Internet for some Pocket PC resources. However, you can find many Microsoft-supplied developer tools at http://www.microsoft.com/windowsmobile/resources/downloads/developer/default.mspx.

The following sections discuss three methods you can use to create applications for the Pocket PC: eMbedded Visual Tools, Visual Studio .NET, and third party tools (emphasizing PocketSOAP). Each of these methods has advantages and disadvantages that make it useful for a particular kind of application. For example, eMbedded Visual Tools provides the best compatibility. Even earlier versions of the Pocket PC can easily use the eMbedded Visual Tools option.

Using Older Microsoft Products

Many developers have used the eMbedded Visual Tools product provided by Microsoft to create applications for the Pocket PC over the years . You have a choice of using either Visual Basic or Visual C++ for the application. As previously mentioned, the main advantage to using eMbedded Visual Tools is compatibility. You don't need anything special to use this solution and it's likely that the resulting application will run on all versions of the Pocket PC as native code (rather than as a Web application).

The biggest disadvantage of this solution is that Microsoft created eMbedded Visual Tools at a time when SOAP wasn't part of the development strategy. You can overcome this issue with some Web services by issuing the request as a specially formatted URL using XML over HTTP (also known as REpresentational State Transfer or REST). Unfortunately, Google doesn't support this option, so you must use a third party product such as PocketSOAP to add SOAP capability to your eMbedded Visual Tools environment. The " Using Third Party Development Products" section of the chapter explains how to use PocketSOAP as part of a JavaScript application ”the technique for using it with eMbedded Visual Tools is similar.

Using the .NET Compact Framework

The .NET Compact Framework offers a lot in the way of SOAP functionality. Using this product helps you create elegant Google Web Services applications with a minimum of fuss and without the need for third party solutions. However, the system you choose to support this application must have the .NET Compact Framework installed, which could result in memory problems for some older Pocket PCs. Listing 9.1 shows a typical example of a search routine that relies on the .NET Compact Framework. You'll find this example in the \Chapter 09\PocketPC folder of the source code located on the Sybex Web site.

Listing 9.1: Creating a Google Request Using the .NET Compact Framework

private void ApplicationTest_Click(object sender, System.EventArgs e) { GoogleSearchService Service; // Search service routine access. GoogleSearchResult Result; // All of the results. ResultElement[] Items; // All of the search items. DataRow DR; // Output data. FrmDetail Details; // Detail form. // Create the search service. Service = new GoogleSearchService(); // Make the call. Result = Service.doGoogleSearch(txtLicense.Text, txtKey.Text, Convert.ToInt32(txtIndex.Text), Convert.ToInt32(txtResults.Text), false, "", false, "", "", ""); // Process the main nodes. txtIndex.Text = Convert.ToString(Result.endIndex + 1); txtEstResults.Text = Result.estimatedTotalResultsCount.ToString(); cbEstExact.Checked = Result.estimateIsExact; // Create the details form. Details = new FrmDetail(); // Create a data set to store the data and associate // it with the grid. Details.DS = new DataSet("dsGoogle"); Details.DS.Tables.Add("Results"); Details.DS.Tables["Results"].Columns.Add("Title"); Details.DS.Tables["Results"].Columns.Add("URL"); Details.DS.Tables["Results"].Columns.Add("Snippet"); Details.dgOutput.DataSource = Details.DS.Tables["Results"]; // Process the result elements. Items = Result.resultElements; foreach (ResultElement Item in Items) { // Add a row. DR = Details.DS.Tables["Results"].NewRow(); // Add the data to the row. DR["Title"] = StringToText(Item.title); DR["URL"] = Item.URL; if (Item.snippet.Length > 0) DR["Snippet"] = StringToText(Item.snippet); else DR["Snippet"] = StringToText(Item.summary); // Display the row on screen. Details.DS.Tables["Results"].Rows.Add(DR); } // Show the details form. Details.Show(); }

 

The example code looks similar to desktop versions of the code, but also reflects the requirements posed by mobile devices. For example, the application uses a main and a detail form to distribute the data. Even with this concession, the data is a little hard to read on the detail page, as shown in Figure 9.9.

Figure 9.9: The detail page shows all of the links returned from the search.

The example begins by creating a new GoogleSearchService object . The next step is to use the doGoogleSearch() method to obtain the search results. In this case, the application lets the user change the license key, the search phrase, the starting index, and the desired number of results. It's important to limit search criteria to those that the user can reasonably use on a small device or must change to make the application work.

Once the code obtains the results, it changes the required main page entries using values from the main nodes. Figure 9.10 shows typical output from this portion of the example.

Figure 9.10: The code automatically updates the main page with the estimated number of results and a new start index.

The code creates the detail form next. The detail form must include two public objects ”a System. Windows .Forms.DataGrid and a System.Data.DataSet. Visual Studio assumes that you want to keep these members private, but you can't access them properly without the change. You also need to add a reference to the System.Windows.Forms.DataGrid namespace to your application since a mobile application doesn't include this control by default.

To store the data in the details form, the code must create the DataSet dynamically and assign it to the DataGrid, dgOutput . Processing the result items is about the same as a desktop application. However, you must keep the display restrictions of a mobile device in mind. This example doesn't include the cached size of the page as part of the output since it's unlikely the mobile device user will store the information locally. The final step is to display the detail form using Details.Show() .

Using Third Party Development Products

You don't have to use a Microsoft solution for your Pocket PC. It's relatively easy to build a fully functional application using just a Web page and some JavaScript when you combine it with a third party SOAP solution. One of the better solutions on the market is PocketSOAP (http://www.pocketsoap.com/). This example relies on PocketSOAP, so you'll need to download a copy to use it. Listing 9.2 shows a typical solution using JavaScript. You'll find this example in the \Chapter 09\PocketPC folder of the source code located on the Sybex Web site.

Listing 9.2: Creating a Google Request Using PocketSOAP

function btnRequest_Click() { var SOAPEnv; // SOAP envelope var Transport; // SOAP transport var Details; // Details array holder // Create the envelope. SOAPEnv = new ActiveXObject("pocketSOAP.Envelope.2"); SOAPEnv.SetMethod("doGoogleSearch", "urn:GoogleSearch"); // Create a parameter to place within the envelope. SOAPEnv.Parameters.Create("key", "Your-License-Key"); SOAPEnv.Parameters.Create( "q", "DataCon Services site:www.mwt.net"); SOAPEnv.Parameters.Create("start", 0); SOAPEnv.Parameters.Create("maxResults", 10); SOAPEnv.Parameters.Create("filter", false); SOAPEnv.Parameters.Create("restrict", ""); SOAPEnv.Parameters.Create("safeSearch", false); SOAPEnv.Parameters.Create("lr", ""); SOAPEnv.Parameters.Create("ie", ""); SOAPEnv.Parameters.Create("oe", ""); // Send the request and receive the data. Transport = new ActiveXObject("pocketSOAP.HTTPTransport.2"); Transport.SOAPAction = "urn:GoogleSearchAction"; Transport.Send("http://api.google.com/search/beta2", SOAPEnv.Serialize()); SOAPEnv.Parse(Transport, "UTF-8"); // Get the Details. RecData = new VBArray(SOAPEnv.Parameters.Item(0).Nodes.Item(4).Value); // Process each URL in turn. Output = ""; for (Counter = RecData.lbound(); Counter <= RecData.ubound(); Counter++) { Output = Output + RecData.getItem(Counter).Nodes.ItemByName("URL").Value + "\r\n"; } window.document.SampleForm1.Results.value = Output; }

 

You could create the required message in a number of ways. The code shows the easiest technique that works reliably with Google Web Services. The example begins by creating a SOAP envelope using the ActiveXObject() method. The specific names of the object will change with PocketSOAP version, so you need to select the correct name for the version you're using (see the PocketSOAP help file).

At this point, you may think that you need to create the body of the message. The code performs this task using a different technique than usual. It adds the body elements to the envelope using the SOAPEnv.Parameters.Create() method. Notice that you pass the various values as the actual types that Google expects, rather than as strings. The serializer performs any required conversions and provides the required type information as part of the SOAP message.

Once the message is complete, the code creates a transport (connection) object. It adds a SOAP action to the transport and then uses the Send() method to send the message. The Send() method requires two arguments ”an endpoint address and the serialized SOAP data found in SOAPEnv .

The code uses the SOAPEnv.Parse() method to retrieve the response from the Transport object. Although it isn't specifically required, you should provide the character encoding argument, UTF-8 as part of the call. Adding this information ensures the application output is usable. Unfortunately, the data within SOAPEnv isn't viewable immediately. The code places the data with RecData by creating a new VBArray based on the value of the <resultElements> node.

Gaining access to the result data as an array means that you can use standard array processing techniques to display the data. In this case, the code uses the ItemByName() method to access the <URL> element of the return data and place it within a string. The concatenated string appears on screen as the result data to the user. You can easily extend this technique to meet other needs, but the example shows the basics of working with PocketSOAP and Google Web Services.

Категории