(Print this page)

VB.Net and the Google API
Published date: Sunday, May 1, 2005
On: Moer and Éric Moreau's web site

Are you, just like me, Googling for any question you have? Every day, I Google for just about anything. Do you know that Google is a verb (see http://www.developer.com/net/asp/article.php/3314371)? If you are still looking for the Caramilk’s secret (a fabulous chocolate in Canada – does it exist elsewhere?), you must see http://members.shaw.ca/b.bogdan/caramilk/cadbury.htm (search Google for: Caramilk secret). Or if you are still searching the “Any” key that older programs were asking to hit to continue, you must read http://web14.compaq.com/falco/detail.asp?FAQnum=FAQ2859 (search Google for: Where do I find the any key on my keyboard). Are you aware that you can use the Google API from your own application? That’s the topic this month column.

Warning

Be warned that at the time of writing, Google is only offering the free beta service at this time. You cannot pay for additional queries. Because it is still in Beta, Google does not guarantee the availability of the service. Since this is an experimental service, Google may take the service down for maintenance, change the APIs in ways that may be incompatible with developer applications, or discontinue the service entirely.

The requirements

Google gives us access to most of their features through a Web Service. This is by far, the easiest way to use it. Before using it, you have to follow 2 simple steps (as seen at http://www.google.com/apis/):
1. Download the developer’s kit from http://www.google.com/apis/download.html. This download is a zip file that contains examples in C#, VB.Net, and Java (what is Java? ;-) ).
2. Create a Google account. When you register, you receive (almost instantaneously by e-mail) your own Google Web APIs license key needed by the web service.

You are now ready to start writing your program. All this is free. The only limitation is that you will be limited to 1000 automated queries per day.

What is a Web service?

Do I really need to explain that? Web services have been much everywhere (but we rarely see them) for a couple of years now.

I will give you my definition of what a web service is:

A piece of an application that runs somewhere (we only need to know the URL to access it) that offers features that we can use through the calls of some methods and that are often returning a result.

All this uses XML to carry the messages (the call and the result). HTTP is used to carry the messages over the wire. There is much more to this technology but this is the basis you absolutely need to know.

You can read a real (and much more exhaustive) definition from http://www.w3.org/TR/wsdl .

So, someone from Google wrote methods in a class and places his component (called a Web Service) on a Web server. We do not know the language that was used and it is not important at all to use it. We don’t even need to know the platform (Windows, Linux, Unix, mainframe, …). The only things we need to know are the URL to reach the Web Service, the methods in it and their signatures (and we need to know what it is doing – returning a weather report or lottery draw results!).

In the case of the Google Web Service, the documentation of methods is available at http://www.google.com/apis/reference.html .

Finally, the last thing I want to tell you about a Web Service is that you don’t have to write Web applications to use them. A Web Service can be used from any type of applications: Windows, Web, Controls, Console, Mobile, … We will create a Windows application to use the Google Web Service in this column.

Start building our app

Go ahead and create a new Windows application.

The first thing we need to do is to create a reference to the Google web service. It is much like creating a reference to a local assembly but since it is a Web Service, we need to create Web reference. From the solution explorer in the VS.Net IDE, find References just under your project name and right-click on it and select “Add Web Reference…”.

Figure 1: Adding a Web Reference

The “Add Web Reference” will then appear. In the address bar, type the URL for the Google Web Service API that is http://api.google.com/GoogleSearch.wsdl and be very cautious because it is in most part case sensitive. Click the Go button. The VS.Net IDE will then search the web to find if there are services at the URL you entered. Once it is found, you can now change the “Web reference name” textbox to something shorter. This name will be used as the namespace of this component in your application. I change mine to simply “Google”. Finally, click the “Add Reference” button.

At the top of your code (in your form), add these 2 lines of declarations:

    Private Const kMyGoogleKey As String = _
        "YourOwnGoogleApiKeyGoesHere"
    Private mobjGoogle As Google.GoogleSearchService = _
        New Google.GoogleSearchService

The Google name space here is the name I just gave when I have created the Web Reference.

We are now ready to create the UI of our application.

Implementing Google's spelling method

The first method of the service we will use is one called doSpellingSuggestion that you can use as a spell checker. Create an interface that looks like this one.

Figure 2: The Spelling section UI

In your button’s click event, add this code:

        txtSpellResult.Text = _
            mobjGoogle.doSpellingSuggestion _
            (kMyGoogleKey, txtSpellSource.Text)
That’s all you need to do! This first part of the application is now ready to run! At run time, if you enter a word that is misspelled like “adress”, and you click the button, a suggestion “address” will appear in the textbox.

Like I have said earlier, the things to know are the name of the methods and what they do and not how it is done or where it goes.

Implementing Google's search method

This feature is a bit more complicated. This time we are using the doGoogleSearch method that has more parameters (parameters are explained at http://www.google.com/apis/reference.html ). You basically send keywords and a collection of result element is returned.

Add some controls to your interface like shown in figure 3.

This is the code you need to add to your button’s click event:

'Ask Google to search for specified keywords
Dim objResult As Google.GoogleSearchResult
objResult = mobjGoogle.doGoogleSearch( _
            kMyGoogleKey, txtSearchKeywords.Text, _
            0, 10, False, "", False, "", "", "")

'Create a datatable to store info returned from Google
Dim dtbResult As DataTable = New DataTable
With dtbResult.Columns
    .Add("Title", GetType(String))
    .Add("URL", GetType(String))
End With

'Store info from Google into the DataTable
Dim x As Google.ResultElement
For Each x In objResult.resultElements
    Dim objRow As DataRow = dtbResult.NewRow
    objRow.Item("Title") = x.title
    objRow.Item("URL") = x.URL
    dtbResult.Rows.Add(objRow)
Next

'Display results in a grid
With grdSearchResult
    .CaptionText = objResult.estimatedTotalResultsCount & _
                    " items found"
    .DataSource = dtbResult
End With

Figure 3: The demo application

Conclusion

This is the basis of what you need to know to get you started on using the Google Web Service API (that is still in Beta remember).

I hope you appreciated the topic and see you next month.


(Print this page)