Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google search via Java Api - Multiple requests

I'm coming from this question.

The following code does not work well:

public static void main(String[] args) throws Exception {
    for (int i = 0; i < 15; i++)
    {
        String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
        String search = "test";
        String charset = "UTF-8";

        URL url = new URL(google + URLEncoder.encode(search, charset));
        Reader reader = new InputStreamReader(url.openStream(), charset);
        GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);

        // Show title and URL of 1st result.
        System.out.println(results.getResponseData().getResults().get(0).getTitle());
        System.out.println(results.getResponseData().getResults().get(0).getUrl());
    }
}

The search query works fine if I run it one time, however in this loop I get a null pointer exception.

Unfortunately I need my program to make several queries :( What can I do?

It returns a NullPointerException at the first results.getResponseData.

like image 965
David Avatar asked Dec 22 '25 14:12

David


1 Answers

This is happening because Google actively blocks suspected terms of service abuse. See section 5.3 here:

http://www.google.com/accounts/TOS

If Google detects that you are issuing search requests via a program without their consent, they don't send back results. Your JSON response will contain this:

{"responseData": null, "responseDetails": "Suspected Terms of Service Abuse. Please see http://code.google.com/apis/errors", "responseStatus": 403}
like image 67
khill Avatar answered Dec 24 '25 03:12

khill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!