delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
496 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


Using the Google web APIs with DelphiFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 6.x (or higher)
Category:
Internet / Web
Skill Level:
Scoring:
Last Update:
02/26/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet SOAP WebServices UDDI WSDL
Times Scored:
6
Visits:
3929
Uploader: Herbert Poltnik
Company:
Reference: Bart Van den Poel
 
Question/Problem/Abstract:
How to access Google via WebServices
Answer:



Introduction, what are the Google Web APIs



The Google Web APIs are api's that use WSDL and SOAP to access Google's search engine. It gives us the possibility to access the vast database of Google to do web searches. At the moment these API's are still in Beta. You are free to try them out if you have a license key and you don't do more than 1000 queries a day. The final version of the API will probably be very different from what it is now, but for experimenting with webservices it is actually great fun. You can download the Google Web APIs from this google site. After downloading the Google Web APIs you also have to make a Google Account to get the key you need to query Google. The source code of the client application we are going to build can be found here . If you unzip the file and try to run the program, it will not work without changing the MY_KEY constant in the file GoogleSearch.pas to the key you got in your mailbox from Google.

Making a Google GUI Application



We are going to make an Application that uses the Google Web APIs to search the web and shows the links so we can click them. To start with, we are going to make a simple Application by Choosing File - New Application. We first save the files. I save my project as GoogleGUI and my Main Form as form_main. I set the caption of the main form to Search Google and change the form name to frmMain. To make our application a little bit more modular I add a datamodule to the project, save it as data_google and name it dtmGoogle. On this datamodule I put an HTTPRio component and call it rioGoogle. This will be used to call the web service. Of course you can also put the HTTPRio component on the form itself, but I want to reuse the datamodule in another project later on to make a webclient.

Now I need the GoogleSearch.wsdl file which describes the Google APIs. You can find this file in the zip file we downloaded from the google site. I first copy the file to our source directory. After that we fill in GoogleSearch.wsdl in the WSDLLocation property of our HTTPRio component. After filling in this property I can select the Service and Port properties from a drop down menu. I select GoogleSearchService and GoogleSearchPort.



Our HTTPRio component is now ready to be used. Now I can import the WSDL file, so Delphi Interfaces will be created to call Google's Web Service. We do this by going to the File - New - Other and choosing WSDL Importer from the Web Services Tab. We first fill in the name of our WSDL file (GoogleSearch.wsdl).



You can see that a few different interfaces , types and methods will be generated after clicking Finish



Be sure to first download the Delphi 6.02 service pack from the Borland site. It contains a lot of important upgrades and even new functionality for web service applications. After importing GoogleSearch.wsdl we will have a GoogleSearch.pas file that we can use in our application. The most important method we are going to use is doGoogleSearch.

We now go back to our dtmGoogle DataModule and create a new public Function in our TdtmGoogle Class called Search. This method has one parameter called text, and returns a GoogleSearchResult variable. We implement this method as follows.

function TdtmGoogle.Search(Text: string): GoogleSearchResult;
var
  originalSeparator : Char;
begin
  originalSeparator := DecimalSeparator;
  try
    DecimalSeparator := '.';
        Result := (rioGoogle as GoogleSearchPort).
    doGoogleSearch(MY_KEY,Text,0,10,False,'',False,'','UTF-8','UTF-8');
    DecimalSeparator := originalSeparator;
  except
    DecimalSeparator := originalSeparator;
    raise;
  end;
end;



Normally the line of code written in blue should be enough, but because of an error in the Soap implementation of Borland Delphi, the program uses , instead of . for decimal seperators. If I only use the blue line, I get this error message if I call the function.



You should also notice that doGoogleSearch has many different parameters I filled in. One of the more important ones is the first one where I have put in the constant MY_KEY. You can find this const defined in the GoogleSearch.pas file I modified. if you try out the example you can download here it will only work if you change this MY_KEY constants value to the key you got in your mailbox from Google. If you need more information about the different parameters of the GoogleSearch method you can look in the documentation that comes with the download.

Now we have to add some functionality to our application so we can search google from our GUI interface. I add a TEdit, call it edtSearch, a TSpeedbutton called btnSearch.



Now I add two event handlers. One OnCreate of the form, and one OnClick for our btnSearch.

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  dtmGoogle := TdtmGoogle.Create(self);
  urlAction := TBrowseURL.Create(self);
end;



In this event handler I Create a TBrowseURL action and load the dtmGoogle datamodule. The TBrowseURL action is an action you normally can find in the ActionList. I only need this one action so I create it at runtime. I defined the urlAction variable in my Class declaration.

type
  TfrmMain = class(TForm)
    edtSearch: TEdit;
    btnSearch: TSpeedButton;
    procedure btnSearchClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure GotoUrl(Sender : TObject);
  private
    urlAction : TBrowseURL;
  public
    { Public declarations }
  end;



I now create the event handler for the onClick event handler

procedure TfrmMain.btnSearchClick(Sender: TObject);
var
  lcv : Integer;
  lbl : TLabel;
  SearchResult : GoogleSearchResult;
  begin
  SearchResult := dtmGoogle.Search(edtSearch.Text);
  for lcv := 0 to pred(Length(SearchResult.resultelements)) do
  begin
    lbl := TLabel.Create(self);
    lbl.Font.Color := clBlue;
    lbl.Font.Style := lbl.Font.Style + [fsUnderline];
    lbl.Left := 12;
    lbl.Top := 45 + lcv * 21;
    lbl.Parent := Self;
    lbl.Caption := SearchResult.resultelements[lcv].URL;
  end;
end;




This code will create a label for each ResultElement of the SearchResult.

If we run this code we will get the following result.




If we again click the button we will create new labels, leaving the other labels were they were, so i add these lines of code to my event handler to remove the labels I created before. I also add an OnClick Event handler to the created labels.

procedure TfrmMain.btnSearchClick(Sender: TObject);
var
  lcv : Integer;
  lbl : TLabel;
  SearchResult : GoogleSearchResult;
begin
  for
   lcv := pred(ComponentCount) downto 0 do
  begin
    if (Components[lcv] is TLabel) and (Components[lcv].Tag = 1)  then
      Components[lcv].Free;
  end;
  SearchResult := dtmGoogle.Search(edtSearch.Text);
  for lcv := 0 to pred(Length(SearchResult.resultelements)) do
  begin
    lbl := TLabel.Create(self);
    lbl.Tag := 1;
    lbl.Font.Color := clBlue;
    lbl.Font.Style := lbl.Font.Style + [fsUnderline];
    lbl.Left := 12;
    lbl.Top := 45 + lcv * 21;
    lbl.Parent := Self;
    lbl.Caption := SearchResult.resultelements[lcv].URL;
    lbl.OnClick := GotoUrl;
  end;
end;



This GotoURL event handler is implemented as follows.




procedure TfrmMain.GotoUrl(Sender: TObject);
begin
  urlAction.URL := (Sender as TLabel).Caption;
  urlAction.Execute;
end;



If we now again try out this program we will be able to click one of the blue labels and our default browser will automatically be started with the selected page.

Our GUI client for Google is finished now. In my next article I will use our dtmGoogle datamodule to create a Web Application implemented as both an ISAPI DLL, a CGI application and an ASP Component.

Conclusion
Using Web Services and the Google API in particular is actually quite easy. The improved WSDL importer of Delphi 6.02 does it's job quite well and makes it possible to quickly add Web Service functionality to internet connected applications or as we will see in a following article, web applications.

About the Author
Bart Van den Poel is a consultant and trainer for PeopleWare nv in Belgium. If you've got any comments on this article you can write him a mail at bart_van_den_poel@peopleware.be.

See the article here:
http://users.pandora.be/rapporten/Articles/GoogleArticle/UsingGoogleAPIWithDelph.html






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Alittle overkill for a simple task..
    Chris Baldwin (Mar 3 2003 3:33PM)

I will say that you chose a good topic to mention about how to access google using SOAP. But there really wasn't a need for you to make it all this complicated and long winded, you've gone in to far too much depth in to your gui design which personally, is a little bizarre.
Keep things simple, just demonstrate your topic in the simpliest way.

Regards...
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)