如何使用HTTP Client Library for Java设置应用程序名称?

时间:2015-11-11 11:17:08

标签: java google-app-engine google-api google-http-client

我正在使用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。

如何使用HTTP Client Library for Java设置应用程序名称?

1 个答案:

答案 0 :(得分:3)

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

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