0

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

}
Community
  • 1
  • 1
Aniket
  • 2,204
  • 5
  • 34
  • 51
  • Can you post some code showing what you've done so far? – austin Mar 30 '13 at 15:17
  • Can you give an example from a real query what you want to show? – NilsH Mar 30 '13 at 15:37
  • @NilsH as shown Answer.java 3rd line my query is "stackoverflow" i need explanation resides in url page instead of only url's which it is currently providin. – Aniket Mar 30 '13 at 15:44
  • Yes, but what "explanation"? Can you give a concret example from a regular google search? – NilsH Mar 30 '13 at 15:45
  • if you search on "stackoverflow" on google you will get few links from that if you check first link same as i am getting in my resultant url's you will get basic information about "stackoverflow" that's the explanation i required. "stackoverflow" is just an example... – Aniket Mar 30 '13 at 15:51

1 Answers1

1

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.

NilsH
  • 13,705
  • 4
  • 41
  • 59