Sample code to call the Yahoo! Local Search API in Java

Source code

import java.util.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;

public class LocalSearch {

  public static void main(String[] args) {
    try {
      String appid = "YOUR APPLICATION ID";
      String query = "Tokyo Tower";
      List<Properties> pois = new LocalSearch(appid).search(query);
      for (Properties p : pois) {
        System.out.println(" " + p.getProperty("lat") + ", " + p.getProperty("lon"));
    } catch (Exception e) {
      System.out.println("ERROR: " + e.toString());

  private final String appid;

  public LocalSearch(String appid) {
    this.appid = appid;

  private Document get(String url, Map<String, String> headers) throws Exception {

    URL urlObj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();

    // timeout
    con.setConnectTimeout(3 * 1000);
    con.setReadTimeout(3 * 1000);

    // HTTP method

    // HTTP request headers
    for (Map.Entry<String, String> header : headers.entrySet()) {
      con.addRequestProperty(header.getKey(), header.getValue());


    InputStream is = con.getInputStream();
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

    return doc;

  private List<Properties> xml2pois(Document doc) throws XPathExpressionException {

    List<Properties> result = new ArrayList<Properties>();

    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList features = (NodeList) xpath.evaluate("YDF/Feature", doc, XPathConstants.NODESET);
    for (int i = 0; i < features.getLength(); i++) {
      Node feature = features.item(i);
      String type = xpath.evaluate("Geometry/Type", feature);
      if (type.equals("point")) {
        String coordinates = xpath.evaluate("Geometry/Coordinates", feature);
        String[] ll = coordinates.split(",");
        Properties p = new Properties();
        p.setProperty("name", xpath.evaluate("Name", feature));
        p.setProperty("lat", ll[1]);
        p.setProperty("lon", ll[0]);
    return result;

  private String encode(Map<String, String> params) throws UnsupportedEncodingException {
    String encoding = "UTF-8";
    List<String> list = new ArrayList<String>();
    for (Map.Entry<String, String> header : params.entrySet()) {
      String key = URLEncoder.encode(header.getKey(), encoding);
      String val = URLEncoder.encode(header.getValue(), encoding);
      list.add(key + "=" + val);
    return String.join("&", list);

  public List<Properties> search(String query) throws Exception {

    String baseURL = "";

    Map<String, String> params = new LinkedHashMap<String, String>();
    params.put("query", query);
    params.put("output", "xml");
    params.put("results", "3");
    params.put("sort", "score");

    String url = baseURL + "?" + encode(params);

    Map<String, String> headers = new LinkedHashMap<String, String>();
    headers.put("User-Agent", "Yahoo AppID: " + appid);

    Document doc = get(url, headers);
    return xml2pois(doc);

Execution result

$ javac 
$ java LocalSearch
Tokyo Tower Observatory club333 Special Stage
 35.65870316, 139.74540779

Tokyo Tower
 34.729707500000, 136.519455833333

Mother Farm CAFE Tokyo Tower
 35.658703055556, 139.745407777778

Reference material

-YOLP \ (map ): Yahoo ! Local search API -Yahoo ! Developer network -Yahoo ! Developer Network: How to use WebAPI \ (GET request ) -Yahoo ! Developer Network

Recommended Posts

Sample code to call the Yahoo! Local Search API in Java
Sample code to call Yahoo! Shopping Product Search (v3) API in Spring RestTemplate
Sample code to call Yahoo! Shopping Product Search (v3) API with HTTP Client API officially introduced from Java 11
Call the Windows Notification API in Java
Sample code to convert List to List <String> in Java Stream
[Java] I tried to implement Yahoo API product search
Sample code to get the values of major SQL types in Java + MySQL 8.0
How to call and use API in Java (Spring Boot)
Guess the character code in Java
Parsing the COTOHA API in Java
Call the super method in Java
Sample code to get the values of major SQL types in Java + Oracle Database 12c
Sample code that uses the Mustache template engine JMustache in Java
Java reference to understand in the figure
Try using the Stream API in Java
How to get the date in java
[Java] New Yahoo! Product Search API Implementation
Sample to unzip gz file in Java
Sample code to get key JDBC type values in Java + H2 Database
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
I called the COTOHA API parser 100 times in Java to measure performance.
Java sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
ChatWork4j for using the ChatWork API in Java
Demo code to recursively search directories in Files.walkFileTree
Code to escape a JSON string in Java
[API] I tried using the zip code search API
Try using the COTOHA API parsing in Java
03. I sent a request from Spring Boot to the zip code search API
Sample source code for finding the least common multiple of multiple values in Java
Conditional branching of the result of SQL statement to search only one in Java
[Java] How to omit the private constructor in Lombok
Source used to get the redirect source URL in Java
[For beginners] Minimum sample to display RecyclerView in Java
Zabbix API in Java
Java classes and instances to understand in the figure
I tried to implement the Euclidean algorithm in Java
Sample code to assign a value in a property file to a field of the expected type
Differences in code when using the length system in Java
Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK
How to correctly check the local HTML file in the browser
Call this cat API of Metadata Co., Ltd. in Java.
Refer to C ++ in the Android Studio module (Java / kotlin)
[Android, Java] Convenient method to calculate the difference in days
I want to use the Java 8 DateTime API slowly (now)
Create a method to return the tax rate in Java
Try hitting the zip code search API with Spring Boot
Sample code to parse date and time with Java SimpleDateFormat
Things to be aware of when writing code in Java
Correct the character code in Java and read from the URL
Tips around the sample to release Java Lambda on CodePipeline
How to derive the last day of the month in Java
How to switch Java in the OpenJDK era on Mac
How to play MIDI files using the Java Sound API
I want to simplify the conditional if-else statement in Java
Java parallelization code sample collection
Java Stream API in 5 minutes
Java in Visual Studio Code
Write Java8-like code in Java8