从Java中的Google Custom Search输出解析的JSON结果

时间:2013-12-07 12:51:08

标签: java json gson google-custom-search

我正在尝试从Java命令行中的Google自定义搜索输出一些结果(标题,网址)进行测试,但我不断收到java.io.EOFException错误。编译器列出了违规行,但即使花了数小时搜索答案,我也无法弄清楚要改变什么。我从Stack Overflow上的现有问题中获取了大部分代码。任何帮助表示赞赏。

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类:

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);
}   
}

错误行:

GResults results = new Gson().fromJson(output, GResults.class);

错误讯息:

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

2 个答案:

答案 0 :(得分:4)

我想在我注意到之前我已经读了大约20次,这是正确的代码

    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正在抛出适当的异常,因为你是逐行阅读并将该行放到gson上进行反序列化。例如,第一行是{或[或“message”:{,并且这不是有效的json JsonSyntax。

享受:)

答案 1 :(得分:1)

我在完成原始JSON输出后终于找到了主要缺陷。 Google的JSON通过[]符号返回数组的“项目”,因此在GResults类中添加List后如下:

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);
}

}

我能够在主要的GSearch类中使用以下命令返回一系列链接:

   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);