javagoogle-app-enginegoogle-apigoogle-http-client

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


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?


Solution

  • You should use the Customsearch.Builder instead of the Customsearch constructor. Example:

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