Google + JSON

5 respostas
X

Pessoal, bom dia.

Estou fazendo buscas no google e capturando seu resultado. Estou usando JSON.
Quando busco apenas uma frase, ou umas 40 frases seguidas, ele funciona perfeitamente.
Quando vou buscar 1000 frases ele me retorna o seguinte.

org.json.JSONException: JSONObject["responseData"] is not a JSONObject.
        at org.json.JSONObject.getJSONObject(JSONObject.java:597)
        at pck.projetocompleto.control.buscadores.GoogleSearch.makeQuery(GoogleSearch.java:58)
        at pck.projetocompleto.control.buscadores.GoogleSearch.<init>(GoogleSearch.java:81)
        at pck.projetocompleto.control.buscadores.GoogleSearch.main(GoogleSearch.java:75)

Alguem tem ideia do que pode ser?
Abço.

5 Respostas

ruivo

Mande imprimir a resposta que o Google retornou. As vezes, ele deu um “access denied” e você está tentando tratar da mesma maneira que o resultado normal.
Caso a resposta esteja ok, tente validá-la usando o jsonLint ( http://jsonlint.com/ )

X

Ruivo, não consegui.

O Erro é aquele mesmo. Segue o Codigo.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pck.projetocompleto.control.buscadores;
 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;      // JSON library from http://www.json.org/java/
import org.json.JSONObject;


public class GoogleSearch {
 
    private void makeQuery(String query) {
        List glink = new ArrayList();
        System.out.println(" Results: "+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/web?hl=pt_br&start=8&rsz=large&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: "+query);

            for (int i = 0; i < ja.length(); i++) {
                //retira os objetos do array
                JSONObject j = ja.getJSONObject(i);
                System.out.println(j.getString("url")); //content, title
                glink.add(j.getString("url"));
            }
        } catch (Exception e) {
            System.err.println("Something went wrong..." );
            e.printStackTrace();
        }
    }

  public static void main(String[] arg0){
      new GoogleSearch();
  }
  
  public GoogleSearch(){
      for (int i = 0; i < 1000; i++){
      makeQuery("momento em que os créditos e empréstimos internacionais diminuem. Geisel anuncia");
      }
  }

}

Não consigo enchergar onde está o erro.
Abço.

X
{"responseData": null, "responseDetails": "Suspected Terms of Service Abuse. Please see http://code.google.com/apis/errors", "responseStatus": 403}
X

Me parece que há uma limitação…
Alguem conhece outra maneira de fazer buscas “sem limites” no google? preciso capturar os links retornador nessa consulta.

ruivo

As respostas para o problema que você está tendo estão todas no site indicado pelo retorno json

Para poder voltar a usar, vc deve fazer o que eles dizem no parágrafo posterior:

http://code.google.com/intl/pt-BR/apis/errors/

Criado 23 de agosto de 2011
Ultima resposta 23 de ago. de 2011
Respostas 5
Participantes 2