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 35 36 37 38 39 40 41 42
| public static String getRslt(String MotsAchercher) throws IOException, JSONException {
String res="";
String key = "ABQIAAAAkeO4DdYEaj26PoyS6Rlq3xTLVLcwd-KOtl51WFBxK6nY0KXPzhSi24WFzsN3gf36ZldX6w-rmXbnFg" ;// votre clé ici
MotsAchercher = MotsAchercher.replaceAll(" ", "%20") ;
// élimination desespaces
URL url = new URL(
"http://ajax.googleapis.com/ajax/services/search/web?v=1.0"+
"&q=" +MotsAchercher+
"&start=1" + // resultat intial entre 0 et 64
"&rsz=large" +
// nbr de résultats retourné large = 8 et small = 4
"&key=" + key ); // votre key de google
URLConnection connection = url.openConnection();
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());
File file1 = new File ("C:\\aze.txt");
FileWriter f1=new FileWriter(file1);
if (json.get("responseStatus").toString().equals("200") ){ // si la réponse est positive
JSONArray ja = json.getJSONObject("responseData").getJSONArray("results");
for (int i = 0; i < ja.length(); i++)
{
JSONObject j = ja.getJSONObject(i);
//System.out.println(j.getInt(""));
//System.out.println(j.getString("titleNoFormatting")); // titre du résultat
//System.out.println(j.getString("url")); // l'url du résultat
// System.out.println(j.getString("content").replaceAll("<b>"," ").replaceAll("</b>"," ")+"\n\n"); // descriptions sous format html
f1.write(j.getString("content").replaceAll("<b>"," ").replaceAll("</b>"," ")+"\n\n");
res=res+j.getString("content").replaceAll("<b>"," ").replaceAll("</b>"," ")+"\n\n";
}
f1.flush();
f1.close ();
}
return res;
} |
Partager