0

I must do a java project
which use Google API for search results.

I found this link : http://www.programcreek.com/2012/05/call-google-search-api-in-java-program/ but this program return only 4 links and i need more.

At the end of this article they say : "This is not a bug, it is designed to be this way. What we can do is to add a parameter to the url “start=#”. If this number is 4, this we have 5-8 results, if the number is 100, we have 101-104 results, so on and so forth. "

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

Mary O
  • 75
  • 11
  • You need to read about how to use parameters in the query section of a URL. Then you will modify the 'address' variable in that code according to the instructions. – Lee Meador May 01 '13 at 21:58

1 Answers1

1

Upon further research, you can't get more than 4 results with a single call. You have to set the start parameter in the URL and query more than once.

Google AJAX API - How do I get more than 4 Results?

Look, it's easy, just do this (working from your linked code):

  public static void main(String[] args) throws IOException {
    List<Result> listResults = new ArrayList<Result>();
    for(int i = 0; i < 10; i++) {
      String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=" + i * 4 + "&q=";
      String query = "java tutorial";
      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);
      listResults.addAll(results.getResponseData.getResults());
    }
Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158
  • It's working only for the first 4 iteration. – Mary O May 02 '13 at 08:51
  • But how can I set the start parameter in the url ? – Mary O May 02 '13 at 13:09
  • Can you give me an example - in java code ? Please :) . – Mary O May 02 '13 at 13:12
  • @OOO look at the link. Just add `&start=4` to the link to get results, call it more than once. – durron597 May 02 '13 at 13:48
  • So the link should be like : URL url = new URL(address + URLEncoder.encode(query, charset) + "&start=4" ); ? -> in this way it doesn't working. And like this : URL url = new URL(address + "&start=4"+ URLEncoder.encode(query, charset) ); -> things doesn't changing. No matter what i do, this returns me only 4 results. – Mary O May 02 '13 at 14:54
  • @OOO You *cannot* get more than 4 results. You have to use a loop and multiple requests. – durron597 May 02 '13 at 14:57
  • That is what i understand when you say loop and multiple request : for(int i=0; i<20; i++) { URL url = new URL(address + URLEncoder.encode(query, charset) ); Reader reader = new InputStreamReader(url.openStream(), charset); GoogleResults results = new Gson().fromJson(reader, GoogleResults.class); System.out.println(results.getResponseData().getResults().get(i).getTitle()); System.out.println(results.getResponseData().getResults().get(i).getUrl()); } – Mary O May 02 '13 at 15:04