【问题标题】:How to set the application name using the HTTP Client Library for Java?如何使用 Java 的 HTTP 客户端库设置应用程序名称?
【发布时间】:2016-02-12 11:26:59
【问题描述】:

我正在使用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 客户端库设置应用程序名称?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回答。将您的评论更新为Customsearch customsearch = new Builder(httpTransport, jsonFactory, null).setApplicationName("your application name").build();,我会接受。
    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 2023-03-08
    相关资源
    最近更新 更多