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 = "";
	String query = "java tutorial";
	String charset = "UTF-8";
	URL url = new URL(address + URLEncoder.encode(query, charset));
	BufferedReader in = new BufferedReader(new InputStreamReader(
	String str;
	while ((str = in.readLine()) != null) {

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: We need to use gson-2.2.1.jar inside of the zip file.

The following code now can return the urls for us.

import java.util.List;
public class TestGoogleSea {
	public static void main(String[] args) throws IOException {
		String address = "";
		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 + "]"; }


total: 4


Title: Top 8 Diagrams for Understanding Java -

Title: Top 10 Methods for Java Arrays -

Title: Eclipse JDT 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 = "";

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.

Title: Junior Ranger Program – Creek Critters | Friends of Tryon Creek

Title: 10 03 13 SCHS vs Sanford, Full program, Creek wins! - YouTube

Title: Backreferences in Java Regular Expressions ...

Title: California, Marin County, McIsaac Ranch, STRAW program creek ...

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 = ""+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.

Category >> Google API  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. Monica on 2012-10-15

    I tried to add “start=20” to the url but it still shows only 4 results.I would truly appreciate it very much if you can tell me exactly how to get more than 4 search results. So what I wrote is
    URL url = new URL(address + “start=20” + URLEncoder.encode(query, charset));

    but it does not work.

    Thank you in advance!

  2. long on 2013-2-4

    I ran into the same problem, but when you use rsz=8 instead of start= 20, things sort out. The maximum value is 8 otherwise the default 4 pages would be used instead.

  3. long on 2013-2-4

    Nice work, BTW, thanks a lot.

  4. User on 2013-5-1

    I don’t understand how can I modify the code so this can return me more then 4 results ?

  5. ryanlr on 2013-5-2

    add a parameter to the url “start=#”. If this number is 4, this we have 5-8 results, etc.

  6. User on 2013-5-4

    Yes, i’m trying but when i put something like : “System.out.println(results.getResponseData().getResults().get(5).getTitle());” this give me : “java.lang.NullPointerException.”

    The address looks like that : “String address = “”+ i + “&q=”; ” where i is 4,8 .. an so on.

  7. Deaw Nibbāna on 2013-10-24

    Could you pleas help me? I don’t understand also about the my result how to more then 4
    please share exsample ?, Thank you for your advance

  8. ryanlr on 2013-10-24

    How to get more results is added in the code.

  9. liujiefeng on 2013-11-15

    The method fromJson(Reader, Class) from the type Gson refers to the missing type GoogleResults :when I run this code ,the result finds the error .why? Thank you for your adivce

  10. Joe Tran on 2013-12-15

    Hi, thanks for your code, it really hepls me a lot. However, I got a question, I want to have about 200 result at once so I change the value at “i<20" to "i<200". But it only shows maximum 100 results. Is there any way to solve it. Thanks.

  11. ryanlr on 2013-12-16

    It is controlled on the Google server side. So I don’t think there is a solution.

  12. Joe Tran on 2013-12-18

    I see. But i’m willing to pay to get more results. I have the key but it still limit at 100 results. Any idea? Thanks.

  13. farhan007 on 2014-1-15

    how could i pass a String to JTextField() to search and from the console output to the JPanel ?? Please help if any idea here….

  14. Chengfeng Lv on 2014-4-20

    Hi, if I just want to get the results from Web(no images or video and so on), what should I do to the address arguments??Is there any way to solve it. Thanks.

  15. jihen on 2014-5-2

    Hi, how can I get the results in Frensh and not on English (url and title) ???

  16. Harminder Singh Deol on 2014-6-28

    How would i go about turning this into a voice search, such that i speak and i get an answer back in voice. Is there one google api that does all that or will it be combination of google speech to text & text to speech API.

  17. shams on 2014-7-9

    Hi, thanks for the nice tutorial. I was trying to add site: option with the search string. When I add some specific websites as the site option it doesn’t show any result, though manual google search shows some entries. What could be the possible reason?

  18. leesf on 2014-8-20

    Hi,if i want to get more results(500),then, i should query the api 125 times, but there is a limit.can i change the 4 results to a bigger number ?

  19. CS Lee on 2014-10-27

    I am looking for a way to parse into google search, a list of values. In other words, to make a query, then save the results into a file; then query the next item in the list, until the list is exhausted. anyone tried that? is there an api for that?

  20. Mr.Choi on 2015-2-1

    Hi, thanks for the tutorial. By the way, may i know is that a way to search news with specified date?


    how to retrieve the summary of each search results as well like google does on displaying search results with a brief description/summary

  22. Rana Prathap B on 2015-6-5

    Hey… a basic question… does google says in any of its documentation to use the below address?
    if not how did we know to get the addess like this?

  23. Zain Ul Haq on 2016-11-11

    I am having a similar problem any progress?

  24. Alishba on 2017-2-22

    why do I get null response data

  25. Ata Bahar on 2017-4-10

    the below api is depracated… use this and look for its protocol to collect data:

Leave a comment