|
Development | .NET Compact Framework
Invoking the Google Web Service on your SmartPhone
Written by W.G. Ryan
[author's bio]
[read 20265 times]
Edited by Derek
Page 1
It's hard to imagine that any software developer
hasn't heard of Google. What
you may or may not be familiar with is their APIs. The
first thing you need to do is register for the service. They'll
give you 1,000 requests a day which is more than enough more
for non-commercial application. If you need more I can't
help you though b/c I wasn't able to find the pricing for
it (by the time you're done reading though, maybe you can
invoke a Google Search for their prices with your Smartphone). Registration
is really easy and it's basically no different from every
other sort of service you register for.
Ok, now I have to warn you about a few
things. The Smartphone isn't the same thing as your desktop. The
framework is called "Compact" for a good reason. This is obscured by the
fact that in many instances, the code is identical to it's desktop
counterpart. Don't lost sight of that fact though. You are in a
resource constrained environment and there's no room for inefficient
code. There aren't 50 events for every control nor are there a lot of
controls in comparison to the desktop. Nor should there be, it just
wouldn't make much sense.
I don't want to get into the guts of web
services and how they work, but in a nutshell, it's pretty much the same as the
desktop. Now, XML isn't the fastest way to access data and in most
instances (particularly on a SmartPhone) it is pretty slow. So why am I
using it? Well, for one thing you don't have many alternatives on the
Smartphone platform. For another it's Google's primary method of exposing
its web service/API..
To set things up, I'm going to create a
basic Smart Device Application and target it to the Smartphone platform. Once you do this, you should see a small form appear in your IDE. Its
size and unique look clearly indicate you are targeting the Smartphone
platform. Like I mentioned, you don't have all of the controls you are
used to and the ones that do exist expose notably different functionality than
you're probably used to dealing with. As such, I'm going to use a
CheckBox control in place of where I'd typically use a CommandButton. When the user clicks on the checkbox, I'll examine its CheckState property and
if it's checked, I'll fire my search.
Since querying Google wouldn't be of much
value unless you can dynamically specify your search criteria, I'm going to
include a TextBox control so the user can provide their search terms. Finally, once the service is invoked, I'll use the ComboBox control to load the
results provided there are any. Once you have your controls loaded, you
could easily expand this to do other things with your results. Air Time
isn't cheap in most cases so this isn't economically the best way to hit data,
but like I mentioned before, your data access options are limited on the
Smartphone and invoking web services is one of your few options. Also
note that you can easily load/read xml using System.Data which has a really
strong implementation even on this platform. Anyway, let's go grab some
data.
The first thing
we need to do after we've registered for the service is
to add a Web Reference to your project. In your Solution
Explorer, right click References and choose the 'Add Web
Reference' option. You can plug in
http://api.google.com/GoogleSearch.wsdl to the
URL box provided. At this point, Visual Studio .NET
should create a proxy class for you provided it was able
to see the .wsdl. It will probably call the proxy something
like com.google.api. After this is created, the behavior
you should see is virtually identical to any other class
you would use or reference. If you look in the Object
Browser -> YourProject.com.google.api you - you'll
see 4 classes (DirectoryCategory, DirectorySearchResult,
DirectorySearchService & ResultElement). If you
see those everything is good to go. Basically, to invoke
and use Google's api, this is pretty much all there is to
it
- private void checkBox1_CheckStateChanged(object
sender, System.EventArgs e)
-
{
-
if(!checkBox1.Checked)return;
-
com.google.api.GoogleSearchResult searchResult = new
com.google.api.GoogleSearchResult();
-
com.google.api.GoogleSearchService g = new com.google.api.GoogleSearchService();
-
searchResult = g.doGoogleSearch("KeyHere", tbCriteria.Text
, 0, 9, false,string.Empty,false, string.Empty,
string.Empty, string.Empty);
-
foreach(com.google.api.ResultElement re in searchResult.resultElements)
-
{
-
cboResults.Items.Add(re.URL);
-
}
-
}
-
-
-
-
All that's
really happening here is checking if the user actually
wants to call the web service and if they do, I instantiated
a new instance of a SearchResult, fire the service using
my private key using doGoogleSearch and then iterate
through the results. As I mentioned, there are
3 other classes you can manipulate w/ the API directly,
but there's a TON of stuff within each of those. In addition, just like there's doGoogleSearch, there's
an Async invocation called BegindoGoogleSearch following
.NET's naming convention for async calls.
This is just a really basic intro into using
the Google API but it lays the ground work for my next article,
integrating the XML features of Infopath, XML Webservices and the Compact
Framework
Back to .NET Compact Framework | [Article Index]
|