Developing with Visual Basic .NET


Visual Basic .NET isn't the same language as Visual Basic 6 ”the two are so different that many developers gave up trying to move code from one to the other. Many of the changes in Visual Basic .NET are actually advantageous, especially for Google Web Services application designers. For example, you have better access to the low-level details of your application. In addition, Visual Basic .NET comes with many Web service support items built in. The following sections discuss how to use Visual Basic .NET to work with Google Web Services.

Tip  

If you don't own Visual Studio .NET, but would like to try it out, you can find a 60-day demonstration version at http://msdn.microsoft.com/vstudio/productinfo/trial/default.aspx. All you need to do is order the CD and install it. You can also find an online trial of Visual Studio .NET at http://msdn.microsoft.com/vstudio/tryit/. In general, the 60-day trial version is a better option when working with Google Web Services.

Creating a Web Reference

Visual Studio .NET introduces a number of automation features. One of these features is the ability to create a reference to a DLL, EXE, service, or other form of executable code both locally and remotely. One type of remote reference relies on Web services. You'll see this feature listed as a Web Reference in Solution Explorer. Use the following steps to create a reference to Google Web Services.

  1. Right-click the project entry in Solution Explorer and choose Add Web Reference from the context menu. You'll see an Add Web Reference dialog box similar to the one shown in Figure 6.7. Notice that this dialog box already has the URL for Google Web Services WSDL file entered.

    click to expand
    Figure 6.7: The Add Web Reference dialog box helps you create a connection to Google Web Services.

  2. Type http://api.google.com/GoogleSearch.wsdl in the URL field as shown in Figure 6.7. Click Go. After a few minutes, the Add Web Reference dialog box will display a list of methods available on Google Web Services as shown in Figure 6.8.

    click to expand
    Figure 6.8: Scroll through the list of Google Web Services methods to learn more about it.

  3. Type a new name for the reference in the Web Reference Name field if desired. The only time you need to perform this task is when you think your application could experience a naming conflict or you want to change the name to match your application better. The examples in this chapter will use the default name of com.Google.api.

  4. Click Add Reference. After a few moments, you'll see a new reference added to Solution Explorer like the one shown in Figure 6.9.

    click to expand
    Figure 6.9: Check Solution Explorer for the new Web reference once the IDE finishes its work.

Adding the Web reference also adds a new file to the project. You'll find this file in the \ Chapter 06 \ VB_NETSearch\VB_NETSearch\WebReferences\com.google.api folder for the example in this section. The References.VB file contains all the information your application needs to interact with Google Web Services. Other languages will create similar folders and reference files. It's interesting to look through this file to see how the Web service reference works. However, make sure you don't change any of the code in the file if you do open it. Changes can cause the Web service interface to stop working.

Using the Google Visual Basic .NET Example

The Google Web Services Kit comes with a Visual Basic .NET example application. You'll find it in the \GoogleAPI\dotnet\Visual Basic folder. The example shows how to perform basic tasks . However, it doesn't really help you exploit Google Web Services. As part of the initial setup for testing the application, I suggest you modify the content of the txtLicenseKey control to include your license key. Making this change means you won't have to constantly enter the license key for every request. Figure 6.10 shows the application.

click to expand
Figure 6.10: Use the example application to learn more about what Google Web Services can do.

The example does test the three major features of Google Web Services: search, spelling check, and cached page display. It doesn't do much with the data. However, you can use the search feature to quickly check the number of results you receive for a particular search string. I found the test application to be faster for testing various combinations of keywords than using the standard interface.

Defining a Search with Visual Basic .NET

Creating a search application with Visual Basic .NET is relatively easy compared to the other languages described in the book. The .NET Framework provides a lot of automation that makes the code easier to create and debug. Listing 6.3 shows a typical example of a search application. You'll find the complete source for this example in the \ Chapter 06 \ VB_NETSearch folder of the source code located on the Sybex Web site.

Listing 6.3: Making a Search Request
start example
 Private Sub btnTest_Click(ByVal sender As System.Object, _                             ByVal e As System.EventArgs) _                             Handles btnTest.Click      Dim Service As GoogleSearchService ' Search service routine access.      Dim Result As GoogleSearchResult   ' All of the results.      Dim Items As ResultElement()       ' All of the search items.      Dim Item As ResultElement          ' A single result.      Dim DR As DataRow                  ' Output data.      ' 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), _                                      cbFiltering.Checked, _                                      txtCountry.Text, _                                      cbSafeSearch.Checked, _                                      txtLanguage.Text, _                                      "", "")      ' Process the main nodes.      txtIndex.Text = Convert.ToString(Result.endIndex + 1)      txtEstResults.Text = Result.estimatedTotalResultsCount.ToString()      cbEstExact.Checked = Result.estimateIsExact      ' Clear the dataset of previous results.      dsGoogle.Tables("SearchResults").Clear()      ' Process the result elements.      Items = Result.resultElements      For Each Item In Items         ' Add a row.         DR = dsGoogle.Tables("SearchResults").NewRow()         ' Add the data to the row.         DR("Title") = StringToText(Item.title)         DR("URL") = Item.URL         If (Item.snippet.Length > 0) Then            DR("SnippetOrSummary") = StringToText(Item.snippet)         Else            DR("SnippetOrSummary") = StringToText(Item.summary)         End If         DR("CachedSize") = Item.cachedSize         ' Display the row on screen.         dsGoogle.Tables("SearchResults").Rows.Add(DR)      Next   End Sub 
end example
 

The code begins by creating a Google client. However, Visual Basic .NET creates a specific client when you create the reference to Google Web Services. This client knows about all of the functions and provides a lot of help in making the call. You'll notice that some of the arguments rely on the Convert.ToInt32() method to convert the text data in the form to an Int32 . The IntelliSense for the Service.doGoogleSearch() automatically alerts you to this requirement.

Unlike many of the other examples in the book, you don't need to use loops to process the main elements of Result . The code accesses these values directly using properties created as the result of reading the Google Web Services WSDL file. You do need to convert some properties for display. For example, the estimatedTotalResultsCount property requires conversion using the ToString() method. You can also use the Convert.ToString() method when needed to accommodate a calculation as shown for the endIndex property.

The individual result items do require loop process. However, you can place the result-Elements property in an array and process the data in a For Each Next loop as shown. Processing the array as a collection ensures you don't have to worry about counter variables .

The example uses a DataSet, dsGoogle , to store values. The DataGrid on the form is linked to dsGoogle , so all of the changes appear automatically. Make sure you view the dsGoogle Tables property to see how the dataset for a Google search is constructed . Notice the special handling the code employs for the Item.snippet property. You can use this technique whenever you need to make a decision about which Google return values to display on screen.

Like many of the other examples in the book, you need to modify the strings that Google provides to Visual Basic .NET. The strings contain special encoding for HTML pages that doesn't work well with desktop applications. Listing 6.4 shows one way to handle the string modifications.

Listing 6.4: Modifying the Google String
start example
 Private Function StringToText(ByVal Input As String) As String      ' Check for bold tag.      Input = Input.Replace("<b>", "")      Input = Input.Replace("</b>", "")      ' Check for the single quote.      Input = Input.Replace("&#39", "'")      ' Check for the <br> tag.      Input = Input.Replace("<br>", "")      Input = Input.Replace("<br/>", "")      Input = Input.Replace("<br />", "")      ' Return the results.      Return Input   End Function 
end example
 

As you can see, all you need is the Replace() method to correct the problem in this case ”no loops or odd string manipulations required. You could place these strings into an XML file, read the file into memory when the application loads, and process the HTML strings in a loop. Using this technique would let you add new strings to the list without recompiling the program. However, the list of strings shown will cover most Google search results. Figure 6.11 shows typical output from this application.

click to expand
Figure 6.11: Typical output for the Visual Basic .NET Search example

It's important to note a few features this application has that most of the previous applications don't provide. First, you can sort the results by clicking the associated column in the DataGrid . The control provides this feature by default, so you don't need to provide any extra coding. Second, this application includes full accessibility support ”something that requires a lot of extra coding when using other languages. By adding custom setups to the DataGrid , you could also present the data in other ways.

Understanding the .NET Difference

Once you build a few Google Web Services applications with Visual Studio 6 and Visual Studio .NET, you begin to notice the .NET difference. Most developers find that Visual Studio .NET languages as a whole reduce the labor required to work with Google Web Services and present the data in a pleasing way. Microsoft went through a lot of effort to increase developer productivity and reduce labor- intensive actions. It would seem that an upgrade to Visual Studio .NET would instantly reap large rewards for any developer making the change.

The problem is that the decision isn't nearly as easy as Microsoft would have you believe. Other sections of the chapter have already discussed obvious benefits of using Visual Studio 6, such as the ability to output native code whenever you need it. Not every Windows machine has the .NET Framework installed and many won't ever have the .NET Framework, which means a .NET application is useless on these machines. However, you must consider several subtle issues when considering the .NET difference.

One of the most important issues is the matter of debugging. You'll notice that the Microsoft SOAP Toolkit contains a Transmission Control Protocol (TCP) tracing tool so that you can see the interaction of your system with Google Web Services. The low-level debugging offered by this tool lets you check the precise format of the message and ensure compatibility problems aren't the cause of a miscommunication . The .NET package offers no such solution, and you'll find that it's very hard to implement such a solution because you don't have full knowledge of the code.

This brings up the second problem with the .NET solution. The IDE generates a lot of the code you need in the background. This feature is good because it decreases development time. It can also reduce programming errors by reducing the amount of custom code a developer creates. However, the negative side is that you don't really know much about that code. When an error occurs, the debugger could take you to an area of that code and you might not have any idea of how to fix the problem. Even if you could fix the problem, the fact that the IDE generates this code automatically means you might revisit the problem more than once because the IDE could break whatever you fixed by regenerating the code.

Whether the .NET solution is the one you need depends on what you want out of your Google Web Services programming experience. The .NET solution is definitely better in more ways that this book will discuss. For example, you can't easily generate the applications found in Chapter 9 using Visual Studio 6 ”it just doesn't include the required projects and support. However, it's important to weigh the cost. This chapter shows both kinds of development side by side so that you can make a good decision on which programming language product to use for your Google Web Services application.




Mining Google Web Services
Mining Google Web Services: Building Applications with the Google API
ISBN: 0782143334
EAN: 2147483647
Year: 2004
Pages: 157

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net