1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public static void main(String[] args)
{
try
{
URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?start=0&rsz=large&v=1.0&q=");
URLConnection connection = url.openConnection();
connection.addRequestProperty("foot", "http://www.google.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
// now have some fun with the results...
System.out.println("Total results = " + json.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount"));
JSONArray ja = json.getJSONObject("responseData").getJSONArray("results");
System.out.println(" Results:");
for (int i = 0; i < ja.length(); i++)
{
System.out.print((i + 1) + ". ");
JSONObject j = ja.getJSONObject(i);
System.out.println(j.getString("titleNoFormatting"));
System.out.println(j.getString("url"));
}
}
catch (Exception e)
{
System.out.println("erreur " + e);
}
} |
Partager