Call Google search API in Java
This post shows how to call google search API in a standard Java program.
1. A Naive Approach
The method below does not require any third-party library, but the result format is not readable, and we can not find what we need through the result text.
public static void main(String[] args) throws IOException { String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; String query = "java tutorial"; String charset = "UTF-8"; URL url = new URL(address + URLEncoder.encode(query, charset)); BufferedReader in = new BufferedReader(new InputStreamReader( url.openStream())); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } |
2. A Better Approach
Luckily, someone already wrote a library for us to convert the Json format to Java class. The library is called "Google Gson", it can be download here: http://code.google.com/p/google-gson/downloads/list. We need to use gson-2.2.1.jar inside of the zip file.
The following code now can return the urls for us.
package google.api.search; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.net.URLEncoder; import java.util.List; import com.google.gson.Gson; public class TestGoogleSea { public static void main(String[] args) throws IOException { String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; String query = "programcreek"; String charset = "UTF-8"; URL url = new URL(address + URLEncoder.encode(query, charset)); Reader reader = new InputStreamReader(url.openStream(), charset); GoogleResults results = new Gson().fromJson(reader, GoogleResults.class); int total = results.getResponseData().getResults().size(); System.out.println("total: "+total); // Show title and URL of each results for(int i=0; i<=total-1; i++){ System.out.println("Title: " + results.getResponseData().getResults().get(i).getTitle()); System.out.println("URL: " + results.getResponseData().getResults().get(i).getUrl() + "\n"); } } } class GoogleResults{ private ResponseData responseData; public ResponseData getResponseData() { return responseData; } public void setResponseData(ResponseData responseData) { this.responseData = responseData; } public String toString() { return "ResponseData[" + responseData + "]"; } static class ResponseData { private List<Result> results; public List<Result> getResults() { return results; } public void setResults(List<Result> results) { this.results = results; } public String toString() { return "Results[" + results + "]"; } } static class Result { private String url; private String title; public String getUrl() { return url; } public String getTitle() { return title; } public void setUrl(String url) { this.url = url; } public void setTitle(String title) { this.title = title; } public String toString() { return "Result[url:" + url +",title:" + title + "]"; } } } |
Output:
Title: ProgramCreek.com
URL: http://www.programcreek.com/
Title: Top 8 Diagrams for Understanding Java - ProgramCreek.com
URL: http://www.programcreek.com/2013/09/top-8-diagrams-for-understanding-java/
Title: Top 10 Methods for Java Arrays - ProgramCreek.com
URL: http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/
Title: Eclipse JDT Tutorials - ProgramCreek.com
URL: http://www.programcreek.com/2011/01/best-java-development-tooling-jdt-and-astparser-tutorials/
Now you have got a list of readable Google search result by using search API and Gson, but you may quickly notice that the result only contains 4 results, but we need more.
This is not a bug, it is designed to be this way. What you can do is to add a parameter to the url "start=#" like the FOLLOWING:
String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=4&q="; |
If start is set to be 4, we have 5th-8th results; if the number is 100, we have 101st-104th results, so on and so forth. The following is results when start is 4.
URL: http://www.tryonfriends.org/junior-ranger-program-creek-critters/
Title: 10 03 13 SCHS vs Sanford, Full program, Creek wins! - YouTube
URL: http://www.youtube.com/watch%3Fv%3Dv0GEi-7LgYg
Title: Programcreek.com: Backreferences in Java Regular Expressions ...
URL: http://www.java.net/story/programcreekcom-backreferences-java-regular-expressions
Title: California, Marin County, McIsaac Ranch, STRAW program creek ...
URL: http://www.davidsanger.com/stockimages/2-216-47.STRAW
3. Put it into a Loop to get more results
You can put this in a loop and change start# in each loop to get more results.
for (int i = 0; i < 20; i = i + 4) { String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start="+i+"&q="; String query = "Programcreek"; String charset = "UTF-8"; URL url = new URL(address + URLEncoder.encode(query, charset)); Reader reader = new InputStreamReader(url.openStream(), charset); GoogleResults results = new Gson().fromJson(reader, GoogleResults.class); // Show title and URL of each results for (int m = 0; m <= 3; m++) { System.out.println("Title: " + results.getResponseData().getResults().get(m).getTitle()); System.out.println("URL: " + results.getResponseData().getResults().get(m).getUrl() + "\n"); } } |
You should be aware that Google does not allow too many requests in a certain amount of time.
<pre><code> String foo = "bar"; </code></pre>
-
Julien Khaleghy
-
Ata Bahar
-
Alishba
-
Zain Ul Haq
-
Rana Prathap B
-
KESHEEKA SHEEKSHA HEERAMUN
-
Mr.Choi
-
CS Lee
-
leesf
-
shams
-
Harminder Singh Deol
-
jihen
-
Chengfeng Lv
-
farhan007
-
Joe Tran
-
ryanlr
-
Joe Tran
-
liujiefeng
-
ryanlr
-
Deaw Nibbāna
-
User
-
ryanlr
-
User
-
long
-
long
-
Monica