0

Apparently google retired the ajax api which I have been using in java for android, I dug through their new API page but I can't find anything that seems to work in java, or any non-web language for that matter.

This is what I had previously:

@Override
protected String doInBackground(String... query) {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + URLEncoder.encode(query[0], "UTF-8")).openStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        reader.close();

        return sb.toString();
    }
    catch(Exception e){e.printStackTrace();
        return "failed to do anything";
    }
}

Simply put, a string is supplied, and appended to the ajax search. The results, in JSON form, are processed into a string builder and then returned as a whole string.

If you attempt to do this now, it returns this result:

{"responseData": null, "responseDetails": "The Google Web Search API is no longer available. Please migrate to the Google Custom Search API (https://developers.google.com/custom-search/)", "responseStatus": 403}

Again, I have read the custom search API but have not found anything that will work outside javascript.

I'd prefer a JSON solution, since that's what I'm using already but I can work with whatever I get.

So, is there some alternative that I can use a similar method with? Or am I going to be forced into another search engine?

//EDIT

Okay, so I now have the fixed code. However it requires an API key, which I can't be posting in my open source project, and its a limited amount of searches unless I pay, so I'm going to be forced to go to a competitor. Here is the fixed code for anyone interested:

@Override
protected String doInBackground(String... query) {
    try {
        String APIKey = "INSERT_YOUR_API_KEY";
        String customSearchEngine = "INSERT_YOUR_CUSTOM_SEARCH_KEY";
        BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("https://www.googleapis.com/customsearch/v1?key=" + APIKey + "&cx=" + customSearchEngine + "&q=" + URLEncoder.encode(query[0], "UTF-8")).openStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        reader.close();

        return sb.toString();
    }
    catch(Exception e){e.printStackTrace();
        return "failed to do anything";
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jonathan C
  • 117
  • 2
  • 9
  • Not that I know any details, but they mention JSON API on that site: https://developers.google.com/custom-search/json-api/v1/overview – jira May 09 '16 at 08:19
  • I saw that, but I couldn't get anything from that which I could run from java to perform a search. – Jonathan C May 09 '16 at 08:46
  • It is a REST api. Google for "Java REST client" to get some examples on how to use it from Java. – jira May 09 '16 at 08:49
  • That's what I needed to look for, mind putting that as an answer so I can up vote it? The page I found was this https://developers.google.com/custom-search/json-api/v1/using_rest#making_a_request – Jonathan C May 09 '16 at 08:54
  • Well, that was more of a hint from me than an answer. But I don't mind getting some points ;) – jira May 09 '16 at 08:56
  • Sometimes a really good hint is a great answer, haha. – Jonathan C May 09 '16 at 08:57
  • custom search will not search the whole web in the paid version. That's limited to the free (developer testing) version https://support.google.com/customsearch/answer/2631040 and http://stackoverflow.com/questions/4082966/what-are-the-alternatives-now-that-the-google-web-search-api-has-been-deprecated (source: comments in that question) – zapl May 09 '16 at 22:04

1 Answers1

1

They mention JSON API on that site here

It is a REST api. Google for "Java REST client" to get some examples on how to use it from Java

jira
  • 3,890
  • 3
  • 22
  • 32