0

I am trying following code downloaded from net. Which is about reading the total search result of google search page.

import com.google.gson.Gson;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLEncoder;


    public class Test {

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

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


            System.out.println(results.getResponseData().getResults().size());

        }
    }

Search Result is

"queries": {
  "nextPage": [
   {
    "title": "Google Custom Search - flowers",
    "totalResults": 10300000,
    "searchTerms": "flowers",
    "count": 10,
    "startIndex": 11,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "cx": "013036536707430787589:_pqjad5hr1a"
   }
  ],

I want to create class `GoogleResults' for above response

user2302288
  • 305
  • 1
  • 5
  • 17
  • 2
    You should post the error. – Stephan Apr 25 '13 at 15:21
  • GoogleResults.class not found. What should be the content of GoogleResults class – user2302288 Apr 25 '13 at 15:22
  • 1
    Where did you get this code from? GoogleResults is not in the package [com.google.gson.Gson](http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html) – Erich Apr 25 '13 at 15:27
  • possible duplicate of [How can you search Google Programmatically Java API](http://stackoverflow.com/questions/3727662/how-can-you-search-google-programmatically-java-api) – Boris the Spider Apr 25 '13 at 15:29

1 Answers1

2

The problem is that you're referring to the GoogleResults class with an unqualified name without importing it.

Either use a fully qualified name or import it. I would post an example but I'm not sure where the class is.

Edit: By searching the code you posted, I found a complete example. It turns out that GoogleResults is not a standard library class, it's a class that you're expected to define yourself.

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 + "]"; }
    }
}
Antimony
  • 37,781
  • 10
  • 100
  • 107