Google search API is now restful. You just hit an URL and you will get the search result in JSON format. For JSON in JAVA go here. Download the free JAVA code. Make a project then build the JAR and now make another project to make the Google search. Here I have used code from the google code.
URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", "http://ifteebuet.blogspot.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
System.out.println(builder.toString());
JSONObject json = new JSONObject(builder.toString());
Iterator i = json.sortedKeys();
//printing out the keys
while (i.hasNext())
System.out.println(i.next().toString());
JSONObject jObject = json.getJSONObject("responseData");
JSONArray myJarray = jObject.getJSONArray("results");
for (int j = 0; j < myJarray.length(); j++) {
//Printing the titles you can print out more data
System.out.println(myJarray.getJSONObject(j).getString("title"));
}
No comments:
Post a Comment