Home  >  Article  >  Java  >  Interpretation of Amap API documentation: Java code to implement real-time bus arrival information query

Interpretation of Amap API documentation: Java code to implement real-time bus arrival information query

PHPz
PHPzOriginal
2023-07-31 12:30:322741browse

Interpretation of the Amap API document: Java code to implement real-time bus arrival information query

With the popularity of smartphones and the development of urban transportation, bus travel has become an indispensable part of modern urban life. . As a leading map service provider in China, Amap provides very powerful bus information functions, which can enable real-time query of bus arrival information. This article will introduce how to implement this function by interpreting the Amap API documentation and providing Java code examples.

First of all, we need to understand the basic usage of Amap API. Before using it, we need to register an Amap developer account and create an application to obtain API usage permissions and keys.

Next, we need to introduce the relevant jar package in order to call the Amap API function in the Java code. These jar packages can be found and downloaded on the "SDK Download" page of the Amap Developer Center.

Once we prepare the development environment, we can start to query real-time bus arrival information. We need to use the "Bus Real-time Inquiry" interface provided by Amap. The specific URL is:

https://restapi.amap.com/v3/bus/stopname?key=your key&city= City name&keywords=Bus stop name

Among them, key is the key we obtained when registering the application, city is the name of the city to be queried, and keywords is the name of the bus stop.

The following is a simple Java code example that demonstrates how to use the Amap API to query real-time bus arrival information:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class BusQuery {

    public static void main(String[] args) {
        try {
            // 准备查询的URL
            String key = "你的密钥";
            String city = "北京";
            String keywords = "天安门";
            String url = "https://restapi.amap.com/v3/bus/stopname?key=" + key + "&city=" + city + "&keywords=" + keywords;
            
            // 发送查询请求
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            
            // 获取查询结果
            int responseCode = connection.getResponseCode();
            if(responseCode == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                
                // 输出查询结果
                System.out.println(response.toString());
            } else {
                System.out.println("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

In this code, we first prepare the query URL, The key, city name and bus stop name we obtained earlier are used. Then, we send a GET request to the URL and get the response result of the request. Finally, we print out the query results.

Through the above Java code example, we can implement a simple function of querying real-time bus arrival information through the Amap API. Of course, this is just a simple example, and we can carry out more complex function expansion and interface design according to specific needs. I hope this article will help you understand the use of the Amap API and realize real-time bus arrival information query.

The above is the detailed content of Interpretation of Amap API documentation: Java code to implement real-time bus arrival information query. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn