Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import java.util.List;
  2.  
  3. import org.json.simple.parser.JSONParser;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.scheduling.annotation.Async;
  6. import org.springframework.stereotype.Service;
  7.  
  8. import com.fasterxml.jackson.databind.JsonNode;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import com.google.api.client.http.GenericUrl;
  11. import com.google.api.client.http.HttpRequest;
  12. import com.google.api.client.http.HttpRequestFactory;
  13. import com.google.api.client.http.HttpResponse;
  14. import com.google.api.client.http.HttpTransport;
  15. import com.google.api.client.http.javanet.NetHttpTransport;
  16.  
  17. @Service
  18. public class GoogleSearchService {
  19.  
  20.  
  21. @Async
  22. public void searchGoogle(List<String> keywords){
  23. try {
  24. ObjectMapper mapper = new ObjectMapper();
  25. HttpTransport httpTransport = new NetHttpTransport();
  26. HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
  27. JSONParser parser = new JSONParser();
  28. GenericUrl url = new GenericUrl("https://kgsearch.googleapis.com/v1/entities:search");
  29. url.put("query", "Kolkata");
  30. url.put("limit", "1");
  31. url.put("indent", "true");
  32. url.put("key", "xxxxxx");
  33. HttpRequest request = requestFactory.buildGetRequest(url);
  34. HttpResponse httpResponse = request.execute();
  35. String responseString = httpResponse.parseAsString();
  36.  
  37. JsonNode node = mapper.readTree(responseString).get("itemListElement").get(0).get("result");
  38. System.out.println(node.get("name"));
  39. System.out.println(node.get("@type"));
  40. System.out.println(node.get("detailedDescription").get("articleBody"));
  41.  
  42. } catch (Exception ex) {
  43. ex.printStackTrace();
  44. }
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement