Reputation: 13
I'm trying to output some results (title, url) from Google Custom Search in Java command line for testing, but I keep getting a java.io.EOFException error. The compiler lists out the offending line, but I can't figure out what to change, even after spending hours searching for an answer. I took most of the code from an existing question here on Stack Overflow. Any help is appreciated.
package google.api.search;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.Gson;
class GSearch {
public static void main(String args[]) throws IOException {
String key = ""; //replace with API key
String qry = ""; // search key word
String cx = ""; //replace with cx
URL url = new URL ("https://www.googleapis.com/customsearch/v1?key=" +key+ "&cx=" +cx+ "&q=" +qry+ "&alt=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept","application/json");
BufferedReader br = new BufferedReader(new InputStreamReader ( ( conn.getInputStream() ) ) );
String output;
{while ((output = br.readLine()) != null){
GResults results = new Gson().fromJson(output, GResults.class);
System.out.println(results);
}
conn.disconnect();
}
}
}
GResults class:
public class GResults {
String title;
String link;
public GResults(String title, String link) {
this.title = title;
this.link = link;
}
public String getTitle(){
return title;
}
public String getLink(){
return link;
}
public void setTitle(String title){
this.title = title;
}
public void setLink(String link){
this.link = link;
}
public String toString(){
return ("Title:%s, Link:%s", title, link);
}
}
Error line:
GResults results = new Gson().fromJson(output, GResults.class);
Error messages:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 2
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at google.api.search.GSearch.main(GSearch.java:26)
Caused by: java.io.EOFException: End of input at line 1 column 2
at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1377)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:483)
at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:403)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.Gson.fromJson(Gson.java:803)
... 4 more
Upvotes: 0
Views: 6477
Reputation: 13
I finally figured out the main flaw after going through the raw JSON output. Google's JSON was returning an array of "items" instead via the []
symbols, so after adding a List in the GResults class as so:
import java.util.List;
public class GResults {
public String link;
public List<GResults> items;
public String getLink(){
return link;
}
public List<GResults> getItems(){
return items;
}
public void setLink(String link){
this.link = link;
}
public void setGroups(List<GResults> items){
this.items = items;
}
public void getThing (int i){
System.out.println(items.get(i));
}
public String toString(){
return String.format("%s", link);
}
}
I was able to return a series of links with the following command in the main GSearch class:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept","application/json");
BufferedReader br = new BufferedReader(new InputStreamReader ( ( conn.getInputStream() ) ) );
GResults results = new Gson().fromJson(br, GResults.class);
for (int i=0; i<10; i++)
results.getThing(i);
Upvotes: 1
Reputation: 3994
I think I read it around 20 times before I noticed, Here is proper code
final StringBuilder builder = new StringBuilder(50);
String output;
while ((output = br.readLine()) != null) {
builder.append(output);
}
final GResults results = new Gson().fromJson(builder.toString(), GResults.class);
Gson is throwing proper exception because you were reading line by line and giving that line to gson for de-serializing. For example first line is { or [ or "message": { , and This is not a valid json JsonSyntax.
Enjoy :)
Upvotes: 5