Newbie - Google API

J

Jacky

Hi,

I have some problems using google API in java. I am using JCreator, and
got the following messages:


GoogleAPIDemo.java:35: cannot resolve symbol
symbol : class GoogleSearch
location: class com.google.soap.search.GoogleA­PIDemo
GoogleSearch s = new GoogleSearch();
^
GoogleAPIDemo.java:35: cannot resolve symbol
symbol : class GoogleSearch
location: class com.google.soap.search.GoogleA­PIDemo
GoogleSearch s = new GoogleSearch();
^
GoogleAPIDemo.java:42: cannot resolve symbol
symbol : class GoogleSearchResult
location: class com.google.soap.search.GoogleA­PIDemo
GoogleSearchResult r = s.doSearch();
^
GoogleAPIDemo.java:61: cannot resolve symbol
symbol : class GoogleSearchFault
location: class com.google.soap.search.GoogleA­PIDemo
} catch (GoogleSearchFault f) {
^
4 errors



I have added the path to googleapi.jar in my JCreator, but still have
the above problems.



Thanks,
Jacky
 
R

Rhino

Is it possible that you forgot the import statements that you need to pick
up the Google classes? If you're sure the imports are present in your source
code, check these things:
- Does the Google jar contain the classes you think it does? Use the jar
command to display the contents of the jar to be sure. Perhaps you
downloaded the wrong jar....
- Is the Google jar really where you think it is? Perhaps you put it one
directory higher or lower than you thought when you added the path to the
classpath.
- Is the path you specified to the Google jar the same as the path that
specifies where the Google jar actually is?

By the way, where did you find this Google API? I've never heard of a Google
API for Java and might be interested in using it myself on a future project.

Rhino


Hi,

I have some problems using google API in java. I am using JCreator, and
got the following messages:


GoogleAPIDemo.java:35: cannot resolve symbol
symbol : class GoogleSearch
location: class com.google.soap.search.GoogleA­PIDemo
GoogleSearch s = new GoogleSearch();
^
GoogleAPIDemo.java:35: cannot resolve symbol
symbol : class GoogleSearch
location: class com.google.soap.search.GoogleA­PIDemo
GoogleSearch s = new GoogleSearch();
^
GoogleAPIDemo.java:42: cannot resolve symbol
symbol : class GoogleSearchResult
location: class com.google.soap.search.GoogleA­PIDemo
GoogleSearchResult r = s.doSearch();
^
GoogleAPIDemo.java:61: cannot resolve symbol
symbol : class GoogleSearchFault
location: class com.google.soap.search.GoogleA­PIDemo
} catch (GoogleSearchFault f) {
^
4 errors



I have added the path to googleapi.jar in my JCreator, but still have
the above problems.



Thanks,
Jacky
 
J

Jacky

Hi Rhino,

Google API is available here:
http://www.google.com/apis/



And the sample source code is here:
================================================
/* Copyright (C) 2002, Google, Inc. */
package com.google.soap.search;
import java.io.*;

/**
* The GoogleAPIDemo is a demonstration of using the Google Web APIs
for
* search and fetching pages from the cache.
*
* @see com.google.soap.search.GoogleSearch
* @see com.google.soap.search.GoogleSearchResult
*/


public class GoogleAPIDemo {
/** Demonstration program to call the Google Web APIs service.
** Usage:<br>
** <tt>java com.google.soap.search.GoogleAPIDemo [key] search
Foo</tt><br>
** <tt>java com.google.soap.search.GoogleAPIDemo [key] cached
http://www.google.com/</tt>
** <tt>java com.google.soap.search.GoogleAPIDemo [key] spell
"britnay spars"</tt>
**/
public static void main(String[] args) {
// Parse the command line
if (args.length != 3) {
printUsageAndExit();
}
String clientKey = "R+9Hco9QFHIHTO1cqDeWV+msvvP1DZTk";
String directive = "search";
String directiveArg = args[2];

// Report the arguments received
System.out.println("Parameters:");
System.out.println("Client key = " + clientKey);
System.out.println("Directive = " + directive);
System.out.println("Args = " + directiveArg);

// Create a Google Search object, set our authorization key
GoogleSearch s = new GoogleSearch();
s.setKey(clientKey);

// Depending on user input, do search or cache query, then print
out result
try {
if (directive.equalsIgnoreCase("search")) {
s.setQueryString(directiveArg);
GoogleSearchResult r = s.doSearch();
System.out.println("Google Search Results:");
System.out.println("======================");
System.out.println(r.toString());
} else if (directive.equalsIgnoreCase("cached")) {
System.out.println("Cached page:");
System.out.println("============");
byte [] cachedBytes = s.doGetCachedPage(directiveArg);
// Note - this conversion to String should be done with
reference
// to the encoding of the cached page, but we don't do that
here.
String cachedString = new String(cachedBytes);
System.out.println(cachedString);
} else if (directive.equalsIgnoreCase("spell")) {
System.out.println("Spelling suggestion:");
String suggestion = s.doSpellingSuggestion(directiveArg);
System.out.println(suggestion);
} else {
printUsageAndExit();
}
} catch (GoogleSearchFault f) {
System.out.println("The call to the Google Web APIs failed:");
System.out.println(f.toString());
}
}

private static void printUsageAndExit() {
System.err.println("Usage: java " +
GoogleAPIDemo.class.getName() +
" <client-key>" +
" (search <query> | cached <url> | spell
<phrase>)");
System.exit(1);
}
}
================================================
 
B

Betty

Rhino said:
Is it possible that you forgot the import statements that you need to pick
up the Google classes? If you're sure the imports are present in your source
code, check these things:
- Does the Google jar contain the classes you think it does? Use the jar
command to display the contents of the jar to be sure. Perhaps you
downloaded the wrong jar....
- Is the Google jar really where you think it is? Perhaps you put it one
directory higher or lower than you thought when you added the path to the
classpath.
- Is the path you specified to the Google jar the same as the path that
specifies where the Google jar actually is?

By the way, where did you find this Google API? I've never heard of a Google
API for Java and might be interested in using it myself on a future
project.

I got this several months ago and followed the instructions, and it worked
fine.
I don't remember where I got it so I just now did a google for it and it is
at
http://www.google.com/apis/
First you get their stuff, then you register with them and get a key from
them,
then you can use their stuff to try it out. There is all the code you need
to
get started and actually do queries and get results. Maybe the OP just made
a
simple error. They limit you to something like 1,000 (or maybe it is 10,000)
queries/day, but if you need more you can work with them (this means you
give them money) to do more. There is also a limit of something like 10
results
returned to you per query, but they tell you how to automatically combine
queries to get more at a time. It's fun.
 
J

Jacky

I can run the program if I type the following in the command line,
java -cp googleapi.jar com.google.soap.search.GoogleAPIDemo <key>
search <keywords>

But I can't compile the sample source codes.
 
P

Peter MacMillan

Jacky said:
Hi,

I have some problems using google API in java. I am using JCreator, and
got the following messages: [snip]

I have added the path to googleapi.jar in my JCreator, but still have
the above problems.

You need to specify the *jar* file, not just the path to it (or *.jar in
a dir with the jar file, or put it in the classpath). The errors you
posted *specifically* mean that the jar hasn't been used.


javac -cp googleapi.jar GoogleAPIDemo.java


java -cp GoogleAPI.jar com.google.soap.search.GoogleAPIDemo mykey search
java


Parameters:
Client key = mykey
Directive = search
Args = java
[and then a whole lot of result data]



Adding a jar in your IDE is OT here - find a forum dedicated to your ide
as it is an ide issue and not a java issue (and you'll have an easier
time finding people who know how the specific IDE works).
 
J

Jacky

thanks!! I was adding the path, til u mentioned about adding a jar!!

I modified it to a jar, and it works!! thanks!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top