高德api空气质量java调用
时间: 2023-11-14 10:04:48 浏览: 95
下面是使用高德API查询空气质量的Java代码示例:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class AirQuality {
private static final String KEY = "您的高德API Key";
private static final String URL = "https://restapi.amap.com/v3/airquality/now";
public static void main(String[] args) throws Exception {
String city = "北京市";
String url = URL + "?key=" + KEY + "&city=" + URLEncoder.encode(city, "UTF-8");
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line).append("\n");
}
reader.close();
System.out.println(response.toString());
}
}
```
需要注意的是,您需要将代码中的`您的高德API Key`替换为您自己的高德API Key。另外,`city`参数可以替换为您需要查询的城市名。查询结果将以JSON格式返回。
阅读全文