2014-02-06 172 views
0

當我嘗試使用谷歌自定義搜索API時,我得到了錯誤。 在服務器Google中,我可以看到我的請求。自定義搜索API

代碼:

public class Main { 
private static final String APPLICATION_NAME = "MyWeb"; 

private static final String API_KEY = "00000"; 
private static final String engine = ("000000"); 


private static HttpTransport httpTransport; 

@SuppressWarnings("unused") 
private static Customsearch client; 


public static void main(String[] args) throws IOException { 

    Customsearch customsearch = new Customsearch(new NetHttpTransport(), new JacksonFactory(),null); 
    try { 
     Customsearch.Cse.List list = customsearch.cse().list("HERE"); 
     list.setKey(API_KEY); 
     list.setCx(engine); 
     Search results = list.execute(); 
     List<Result> items = results.getItems(); 

     for(Result result:items) 
     { 
      System.out.println("Title:"+result.getHtmlTitle()); 
     } 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    } 
} 

錯誤:

Exception in thread "main" java.lang.AbstractMethodError: com.google.api.client.json.JsonFactory.createJsonParser(Ljava/io/InputStream;Ljava/nio/charset/Charset;)Lcom/google/api/client/json/JsonParser; 
    at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85) 
    at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81) 
    at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459) 
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) 
    at com.korney.Main.main(Main.java:44) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 

Process finished with exit code 1 
+0

你在類路徑上有一個API,你是否也有一個實現?如果這不是問題,請發佈一些代碼請... – Caleryn

+0

你用戶'JsonFactory'類 – lakshman

回答

0

由於還建議由拉克什曼評論,這幾乎可以肯定是由於你Customsearch建設缺乏com.google.api.client.json.JsonFactory的實現對象,您目前將其設置爲null。嘗試添加這個實現。

目前有一些JavaDoc在http://javadoc.google-api-java-client.googlecode.com/hg-history/69fdf148d5a867b841f4146132343304e73cf9c1/apis/customsearch/v1/com/google/api/services/customsearch/Customsearch.html爲Customsearch對象,它可能會幫助你。