我正在使用HTTP Client Library for Java访问Google自定义搜索API并执行搜索。

 String key = "...";
 String cx = "...";
 String query = "...";
 // Set up the HTTP transport and JSON factory
 HttpTransport httpTransport = new NetHttpTransport();
 JsonFactory jsonFactory = new JacksonFactory();

 Customsearch customsearch = new Customsearch(httpTransport, jsonFactory,null);
 List<Result> resultList = null;
 try {
       Customsearch.Cse.List list = customsearch.cse().list(query);
       list.setKey(key);
       list.setCx(cx);
       Search results = list.execute();
 }catch (Exception e) {
       LOG.debug("Exception: " + e);
 }


尽管可以,但我总是收到以下警告:


  com.google.api.client.googleapis.services.AbstractGoogleClient
  警告:未设置应用程序名称。调用Builder#setApplicationName。


如何使用Java的HTTP客户端库设置应用程序名称?

最佳答案

您应该使用Customsearch.Builder而不是Customsearch构造函数。例:

Customsearch customsearch = new Builder(httpTransport, jsonFactory, null).setApplicationName("your application name").build();

07-24 09:34