我正在尝试使用google自定义搜索API for java但在某些原因它不起作用。这是我的java代码:
String searchText = "football";
String key="mykey";
String cx = "mygeneratedcx";
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
}
};
JsonFactory jsonFactory = new JacksonFactory();
Customsearch custom = new Customsearch(new NetHttpTransport(), jsonFactory, httpRequestInitializer);
Customsearch.Cse.List list = custom.cse().list("football");
list.setCx(cx);
list.setKey(key);
Search result = list.execute();
List listResult = (List) result.getItems();
Result first = (Result) listResult.get(0);
而且这是我使用api的依赖:
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-http</artifactId>
<version>1.2.3-alpha</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-customsearch</artifactId>
<version>v1-rev30-1.17.0-rc</version>
</dependency>
我已经看过一些例子,但它们并不符合我得到的api版本。 当上面的代码执行时,我收到了以下异常:
线程“main”中的异常java.lang.NoSuchMethodError:com.google.api.client.util.Strings.isNullOrEmpty(Ljava / lang / String;)Z 在com.google.api.client.googleapis.services.AbstractGoogleClient
如果有人使用这个google自定义搜索api for java,我会非常感谢您的帮助!
提前谢谢!
答案 0 :(得分:0)
问题解决了。我改变了谷歌api客户端的依赖性:
com.google.http客户端
谷歌-HTTP客户端 - 杰克逊
1.15.0-RC