Aniket
Aniket

Reputation: 2214

Search Google result instead only URL using Java

I am working on java application where I need results from Google. So I have used help given in 1'st answer on this page search Google Programmatically Java API

From successfully running this program I am getting URL's where I get my result this is okay.

But I need explanation of searched query instead of URL's.

So how do I get resultant answer from searched query, any help is appreciated.

Answer.java is

public static void main(String[] args) throws Exception {
    String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
    String search = "stackoverflow";
    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());
}

GoogleResults.java is

public 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 + "]"; }
    }

}

Upvotes: 0

Views: 1260

Answers (1)

NilsH
NilsH

Reputation: 13821

If you paste the search URL in a browser, you can see how the JSON result is formatted. Your GoogleResult class maps properties from this JSON to properties in this class. To extract more information from the search result, you can just add the appropriate properties to your class, and it should be handled by the JSON parser transforming the JSON result to your GoogleResult class. So if content from the JSON result is the "explanation" you're looking for, your Result class would look like this:

static class Result {
    private String url;
    private String title;
    private String content;
    public String getUrl() { return url; }
    public String getTitle() { return title; }
    public String getContent() {return content; }
    public void setUrl(String url) { this.url = url; }
    public void setTitle(String title) { this.title = title; }
    public void setContent(String content) { this.content = content; }
    public String toString() { return "Result[url:" + url +",title:" + title + ",content:" + content + "]"; }
}    

Then you can use result.getContent() to get the description of the search result. You can use the same process to extract any data from the JSON result that you want.

Upvotes: 1

Related Questions