Question

I'm requested to write an inverted index, so I would like as a start to write a java program which google searches a word and putting the results into an arraylist.

Here's my code:

String search = "Dan";
String google = "http://www.google.com/cse/publicurl?cx=012216303403008813404:kcqyeryhhm8&q=" + search;
URL url = new URL(google);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader reader = new BufferedReader(new InputStreamReader(
    (conn.getInputStream())));
// Gather the results to a String array
List<String> resultsList = new ArrayList<String>();
String r;
while ((r = reader.readLine()) != null)
    resultsList.add(r);
conn.disconnect();
System.out.println("Google Search for: " + search + " Is Done!");

The programs runs with no crashes in the middle, but I get only a source code of a page (which does not contain any links).

What do I need to change in the code? Maybe I need a whole different method?

Était-ce utile?

La solution

If you want to use google search in your app you should use Google's API for that:

Custom search API

You get search results in JSON format.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top