How to get the number of recent news using Google News api

Discussion in 'Google API' started by bresiva, Jun 14, 2010.

  1. #1
    I want get the recent results (for example, the news in the past week) from google news, especially the number of the results, using Google News api.
    Referring to the code onhttp://www.ajaxlines.com/ajax/stuff/article/using_google_is_ajax_search_api_with_java.php
    , I wrote the java code:

     private void makeQuery(String query) {
    
      System.out.println(" Querying for " + query);
    
      try
      {
       // Convert spaces to +, etc. to make a valid URL
       query = URLEncoder.encode(query, "UTF-8");
    
       URL url = new URL("http://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=" + query);
       URLConnection connection = url.openConnection();
       connection.addRequestProperty("Referer", HTTP_REFERER);
    
       // Get the JSON response
       String line;
       StringBuilder builder = new StringBuilder();
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(connection.getInputStream()));
       while((line = reader.readLine()) != null) {
        builder.append(line);
       }
    
       String response = builder.toString();
       JSONObject json = new JSONObject(response);
    
       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.err.println("Something went wrong...");
       e.printStackTrace();
      }
     }
    Code (markup):
    It can only return all the result.
    On http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje, I cann't find the specific argument to return the results only in certain period (e.g. last week).

    Is there anyway to achieve this?
     
    bresiva, Jun 14, 2010 IP