hello;

I want to develop a small application and I need only the number of search 
results. I tried to use Google API for search but i have this error when i 
excecute my program


Exception in thread "main" com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at 
line 1 column 20
    at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.Gson.fromJson(Gson.java:795)
    at com.google.gson.Gson.fromJson(Gson.java:734)
    at projetmygoogle.test.GoogleResults1.main(GoogleResults1.java:56)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was 
STRING at line 1 column 20
    at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
    at 
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
    ... 3 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Thank you  for helping me finf a solution.

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Google AJAX APIs" group.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
To view this message on the web, visit
https://groups.google.com/d/msgid/google-ajax-search-api/be26da87-cd72-47fb-bfe3-20f92e7a34be%40googlegroups.com?hl=en_US
For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


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

import com.google.gson.Gson;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import org.json.JSONObject;

/**
 *
 * @author sotra
 */
public class GoogleResults1 {
  

    private ResponseData responseData;
    public ResponseData getResponseData() { return responseData; }
    public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
    @Override
    public String toString() { return "ResponseData[" + responseData + "]"; }

    static class ResponseData {
        private List<Result> results=null;
        public List<Result> getResults() { return results; }
        public void setResults(List<Result> results) { this.results = results; }
        @Override
        public String toString() { return "Results[" + results + "]"; }
    }

    static class Result {
        private String url;
        private String title;
        private String content;
        public String getUrl() { return url; }
        public String getTitle() { return title; }
        public String getContent() { return content; }
        public void setUrl(String url) { this.url = url; }
        
        public void setTitle(String title) { this.title = title; }
        @Override
        public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
    }


 public static void main(String[] args) throws Exception {
    String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";;
    String search = "ga?l dias";
    String charset = "UTF-8";

    URL url = new URL(google + URLEncoder.encode(search, charset));
    Reader reader = new InputStreamReader(url.openStream(), charset);
    GoogleResults1 results = new Gson().fromJson(reader, GoogleResults1.class);   
   
    
     for (int i=0 ;i <results.getResponseData().getResults().size(); i++)
    {
   //System.out.println(results.getResponseData().getResults().get(i).getTitle());
   // System.out.println(results.getResponseData().getResults().get(i).getUrl());
    System.out.println(results.getResponseData().getResults().get(i).getContent().toString());
    System.out.println(" resultat suivant"+"\n");
           
    }    
   
    
 }

}

    

Reply via email to