如何使用HTTP客户端库for Java设置应用程序名称? [英] How to set the application name using the HTTP Client Library for Java?
问题描述
我使用 HTTP客户端库for Java 来访问Google Custom Search API并执行搜索。
String key =...;
String cx =...;
String query =...;
//设置HTTP传输和JSON工厂
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Customsearch customsearch = new Customsearch(httpTransport,jsonFactory,null);
清单<结果> resultList = null;
尝试{
Customsearch.Cse.List list = customsearch.cse()。list(query);
list.setKey(key);
list.setCx(cx);
搜索结果= list.execute();
} catch(Exception e){
LOG.debug(Exception:+ e);
}
尽管它有效,但我始终得到这样的警告:
com.google.api.client.googleapis.services.AbstractGoogleClient
警告:未设置应用程序名称。 Call Builder#setApplicationName。
如何使用HTTP客户端库设置应用程序名称?
$ b $您应该使用
/latest/com/google/api/services/customsearch/Customsearch.Builder.htmlrel =nofollow> Customsearch.Builder 而不是Customsearch构造函数。例如:
$ pre $ Customsearch customsearch = new Builder(httpTransport,jsonFactory,null).setApplicationName(your application name)。build );
I am using the HTTP Client Library for Java to access the Google Custom Search API and perform a search.
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);
}
Although it works, I always get this warning:
com.google.api.client.googleapis.services.AbstractGoogleClient WARNING: Application name is not set. Call Builder#setApplicationName.
How can I set the application name using the HTTP Client Library for Java?
You should use the Customsearch.Builder instead of the Customsearch constructor. Example:
Customsearch customsearch = new Builder(httpTransport, jsonFactory, null).setApplicationName("your application name").build();
这篇关于如何使用HTTP客户端库for Java设置应用程序名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!