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

How to set the application name using the HTTP Client Library for Java?

我正在使用 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 WARNING: Application name is not set. Call Builder#setApplicationName.

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

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

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