支持省份清单
支持城市清单
查询城市实时空气质量
查询站点实时空气质量
查询近14天空气质量
查询近24小时空气质量
请求Header:
名称 | 值 | |
---|---|---|
Content-Type | application/x-www-form-urlencoded |
请求参数说明:
名称 | 必填 | 类型 | 说明 | |
---|---|---|---|---|
key | 是 | string | 接口请求key,在个人中心->数据中心->我的API进行查看 |
请求代码示例:
curl -k -i "http://apis.juhe.cn/fapigw/air/provinces?key=key"
<?php
/**
* 1906-支持省份清单 - 代码参考(根据实际业务情况修改)
*/
// 基本参数配置
$apiUrl = "http://apis.juhe.cn/fapigw/air/provinces"; // 接口请求URL
$method = "GET"; // 接口请求方式
$headers = ["Content-Type: application/x-www-form-urlencoded"]; // 接口请求header
$apiKey = "您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
$requestParams = [
'key' => $apiKey,
];
$requestParamsStr = http_build_query($requestParams);
// 发起接口网络请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $apiUrl . '?' . $requestParamsStr);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (1 == strpos("$" . $apiUrl, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$response = curl_exec($curl);
$httpInfo = curl_getinfo($curl);
curl_close($curl);
// 解析响应结果
$responseResult = json_decode($response, true);
if ($responseResult) {
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
var_dump($responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
// var_dump($httpInfo);
var_dump("请求异常");
}
import requests
# 1906-支持省份清单 - 代码参考(根据实际业务情况修改)
# 基本参数配置
apiUrl = 'http://apis.juhe.cn/fapigw/air/provinces' # 接口请求URL
apiKey = '您申请的调用APIkey' # 在个人中心->我的数据,接口名称上方查看
# 接口请求入参配置
requestParams = {
'key': apiKey,
}
# 发起接口网络请求
response = requests.get(apiUrl, params=requestParams)
# 解析响应结果
if response.status_code == 200:
responseResult = response.json()
# 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
print(responseResult)
else:
# 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
print('请求异常')
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func main() {
// 基本参数配置
apiUrl := "http://apis.juhe.cn/fapigw/air/provinces"
apiKey := "您申请的调用APIkey"
// 接口请求入参配置
requestParams := url.Values{}
requestParams.Set("key", apiKey)
// 发起接口网络请求
resp, err := http.Get(apiUrl + "?" + requestParams.Encode())
if err != nil {
fmt.Println("网络请求异常:", err)
return
}
defer resp.Body.Close()
var responseResult map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&responseResult)
if err != nil {
fmt.Println("解析响应结果异常:", err)
return
}
fmt.Println(responseResult)
}
using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Common_API_Test.Test_Demo
{
class Csharp_get
{
static void Main(string[] args)
{
string url = "http://apis.juhe.cn/fapigw/air/provinces";
string apiKey = "您申请的调用APIkey";
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("key", apiKey);
using (WebClient client = new WebClient())
{
string fullUrl = url + "?" + string.Join("&", data.Select(x => x.Key + "=" + x.Value));
try
{
string responseContent = client.DownloadString(fullUrl);
dynamic responseData = JsonConvert.DeserializeObject(responseContent);
if (responseData != null)
{
Console.WriteLine("Return Code: " + responseData["error_code"]);
Console.WriteLine("Return Message: " + responseData["reason"]);
}
else
{
Console.WriteLine("json解析异常!");
}
}
catch (Exception)
{
Console.WriteLine("请检查其它错误");
}
}
}
}
}
const axios = require('axios'); // npm install axios
// 基本参数配置
const apiUrl = 'http://apis.juhe.cn/fapigw/air/provinces'; // 接口请求URL
const apiKey = '您申请的调用APIkey'; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
const requestParams = {
key: apiKey,
};
// 发起接口网络请求
axios.get(apiUrl, {params: requestParams})
.then(response => {
// 解析响应结果
if (response.status === 200) {
const responseResult = response.data;
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
console.log(responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
console.log('请求异常');
}
})
.catch(error => {
// 网络请求失败,可以根据实际情况进行处理
console.log('网络请求失败:', error);
});
package cn.juhe.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class JavaGet {
public static void main(String[] args) throws Exception {
String apiKey = "你申请的key";
String apiUrl = "http://apis.juhe.cn/fapigw/air/provinces";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}
// 基本参数配置
NSString *apiUrl = @"http://apis.juhe.cn/fapigw/air/provinces"; // 接口请求URL
NSString *apiKey = @"您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
NSDictionary *requestParams = @{
@"key": apiKey,
};
// 发起接口网络请求
NSURLComponents *components = [NSURLComponents componentsWithString:apiUrl];
NSMutableArray *queryItems = [NSMutableArray array];
[requestParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}];
components.queryItems = queryItems;
NSURL *url = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// 网络请求异常处理
NSLog(@"请求异常");
} else {
NSError *jsonError;
NSDictionary *responseResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError) {
// 网络请求成功处理
// NSLog(@"%@", [responseResult objectForKey:@"error_code"]);
NSLog(@"%@", responseResult);
} else {
// 解析结果异常处理
NSLog(@"解析结果异常");
}
}
}];
[task resume];
返回参数说明:
名称 | 类型 | 说明 | |
---|---|---|---|
error_code | int | 返回码 | |
reason | string | 返回结果描述 | |
result | array | 返回结果集 | |
Id | string | 省份ID | |
ProvinceCode | string | 省份CODE | |
ProvinceName | string | 省份代码 | |
ProvinceJC | string | 省份简码 |
JSON返回示例:JSON在线格式化工具 >
{
"reason": "success",
"result": [
{
"Id": "1",
"ProvinceCode": "110000",
"ProvinceName": "北京",
"ProvinceJC": "BJ"
},
{
"Id": "2",
"ProvinceCode": "120000",
"ProvinceName": "天津",
"ProvinceJC": "TJ"
},
{
"Id": "3",
"ProvinceCode": "130000",
"ProvinceName": "河北",
"ProvinceJC": "HB"
},
{
"Id": "4",
"ProvinceCode": "140000",
"ProvinceName": "山西",
"ProvinceJC": "SX"
},
{
"Id": "5",
"ProvinceCode": "150000",
"ProvinceName": "内蒙古",
"ProvinceJC": "NMG"
},
{
"Id": "6",
"ProvinceCode": "210000",
"ProvinceName": "辽宁",
"ProvinceJC": "LN"
},
{
"Id": "7",
"ProvinceCode": "220000",
"ProvinceName": "吉林",
"ProvinceJC": "JL"
},
{
"Id": "8",
"ProvinceCode": "230000",
"ProvinceName": "黑龙江",
"ProvinceJC": "HLJ"
},
{
"Id": "9",
"ProvinceCode": "310000",
"ProvinceName": "上海",
"ProvinceJC": "SH"
},
{
"Id": "10",
"ProvinceCode": "320000",
"ProvinceName": "江苏",
"ProvinceJC": "JS"
},
{
"Id": "11",
"ProvinceCode": "330000",
"ProvinceName": "浙江",
"ProvinceJC": "ZJ"
},
{
"Id": "12",
"ProvinceCode": "340000",
"ProvinceName": "安徽",
"ProvinceJC": "AH"
},
{
"Id": "13",
"ProvinceCode": "350000",
"ProvinceName": "福建",
"ProvinceJC": "FJ"
},
{
"Id": "14",
"ProvinceCode": "360000",
"ProvinceName": "江西",
"ProvinceJC": "JX"
},
{
"Id": "15",
"ProvinceCode": "370000",
"ProvinceName": "山东",
"ProvinceJC": "SD"
},
{
"Id": "16",
"ProvinceCode": "410000",
"ProvinceName": "河南",
"ProvinceJC": "HN"
},
{
"Id": "17",
"ProvinceCode": "420000",
"ProvinceName": "湖北",
"ProvinceJC": "HB"
},
{
"Id": "18",
"ProvinceCode": "430000",
"ProvinceName": "湖南",
"ProvinceJC": "HN"
},
{
"Id": "19",
"ProvinceCode": "440000",
"ProvinceName": "广东",
"ProvinceJC": "GD"
},
{
"Id": "20",
"ProvinceCode": "450000",
"ProvinceName": "广西",
"ProvinceJC": "GX"
},
{
"Id": "21",
"ProvinceCode": "460000",
"ProvinceName": "海南",
"ProvinceJC": "HN"
},
{
"Id": "22",
"ProvinceCode": "500000",
"ProvinceName": "重庆",
"ProvinceJC": "ZQ"
},
{
"Id": "23",
"ProvinceCode": "510000",
"ProvinceName": "四川",
"ProvinceJC": "SC"
},
{
"Id": "24",
"ProvinceCode": "520000",
"ProvinceName": "贵州",
"ProvinceJC": "GZ"
},
{
"Id": "25",
"ProvinceCode": "530000",
"ProvinceName": "云南",
"ProvinceJC": "YN"
},
{
"Id": "26",
"ProvinceCode": "540000",
"ProvinceName": "西藏",
"ProvinceJC": "XC"
},
{
"Id": "27",
"ProvinceCode": "610000",
"ProvinceName": "陕西",
"ProvinceJC": "SX"
},
{
"Id": "28",
"ProvinceCode": "620000",
"ProvinceName": "甘肃",
"ProvinceJC": "GS"
},
{
"Id": "29",
"ProvinceCode": "630000",
"ProvinceName": "青海",
"ProvinceJC": "QH"
},
{
"Id": "30",
"ProvinceCode": "640000",
"ProvinceName": "宁夏",
"ProvinceJC": "NX"
},
{
"Id": "31",
"ProvinceCode": "650000",
"ProvinceName": "新疆",
"ProvinceJC": "XJ"
}
],
"error_code": 0
}
请求Header:
名称 | 值 | |
---|---|---|
Content-Type | application/x-www-form-urlencoded |
请求参数说明:
名称 | 必填 | 类型 | 说明 | |
---|---|---|---|---|
key | 是 | string | 接口请求key,在个人中心->数据中心->我的API进行查看 | |
pId | 是 | string | 省份ID |
请求代码示例:
curl -k -i "http://apis.juhe.cn/fapigw/air/citys?key=key&pId=xxx"
<?php
/**
* 1907-支持城市清单 - 代码参考(根据实际业务情况修改)
*/
// 基本参数配置
$apiUrl = "http://apis.juhe.cn/fapigw/air/citys"; // 接口请求URL
$method = "GET"; // 接口请求方式
$headers = ["Content-Type: application/x-www-form-urlencoded"]; // 接口请求header
$apiKey = "您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
$requestParams = [
'key' => $apiKey,
'pId'=> 'xxx',
];
$requestParamsStr = http_build_query($requestParams);
// 发起接口网络请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $apiUrl . '?' . $requestParamsStr);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (1 == strpos("$" . $apiUrl, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$response = curl_exec($curl);
$httpInfo = curl_getinfo($curl);
curl_close($curl);
// 解析响应结果
$responseResult = json_decode($response, true);
if ($responseResult) {
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
var_dump($responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
// var_dump($httpInfo);
var_dump("请求异常");
}
import requests
# 1907-支持城市清单 - 代码参考(根据实际业务情况修改)
# 基本参数配置
apiUrl = 'http://apis.juhe.cn/fapigw/air/citys' # 接口请求URL
apiKey = '您申请的调用APIkey' # 在个人中心->我的数据,接口名称上方查看
# 接口请求入参配置
requestParams = {
'key': apiKey,
'pId': 'xxx',
}
# 发起接口网络请求
response = requests.get(apiUrl, params=requestParams)
# 解析响应结果
if response.status_code == 200:
responseResult = response.json()
# 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
print(responseResult)
else:
# 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
print('请求异常')
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func main() {
// 基本参数配置
apiUrl := "http://apis.juhe.cn/fapigw/air/citys"
apiKey := "您申请的调用APIkey"
// 接口请求入参配置
requestParams := url.Values{}
requestParams.Set("key", apiKey)
requestParams.Set("pId", "xxx")
// 发起接口网络请求
resp, err := http.Get(apiUrl + "?" + requestParams.Encode())
if err != nil {
fmt.Println("网络请求异常:", err)
return
}
defer resp.Body.Close()
var responseResult map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&responseResult)
if err != nil {
fmt.Println("解析响应结果异常:", err)
return
}
fmt.Println(responseResult)
}
using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Common_API_Test.Test_Demo
{
class Csharp_get
{
static void Main(string[] args)
{
string url = "http://apis.juhe.cn/fapigw/air/citys";
string apiKey = "您申请的调用APIkey";
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("key", apiKey);
data.Add( "pId", "xxx");
using (WebClient client = new WebClient())
{
string fullUrl = url + "?" + string.Join("&", data.Select(x => x.Key + "=" + x.Value));
try
{
string responseContent = client.DownloadString(fullUrl);
dynamic responseData = JsonConvert.DeserializeObject(responseContent);
if (responseData != null)
{
Console.WriteLine("Return Code: " + responseData["error_code"]);
Console.WriteLine("Return Message: " + responseData["reason"]);
}
else
{
Console.WriteLine("json解析异常!");
}
}
catch (Exception)
{
Console.WriteLine("请检查其它错误");
}
}
}
}
}
const axios = require('axios'); // npm install axios
// 基本参数配置
const apiUrl = 'http://apis.juhe.cn/fapigw/air/citys'; // 接口请求URL
const apiKey = '您申请的调用APIkey'; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
const requestParams = {
key: apiKey,
pId: 'xxx',
};
// 发起接口网络请求
axios.get(apiUrl, {params: requestParams})
.then(response => {
// 解析响应结果
if (response.status === 200) {
const responseResult = response.data;
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
console.log(responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
console.log('请求异常');
}
})
.catch(error => {
// 网络请求失败,可以根据实际情况进行处理
console.log('网络请求失败:', error);
});
package cn.juhe.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class JavaGet {
public static void main(String[] args) throws Exception {
String apiKey = "你申请的key";
String apiUrl = "http://apis.juhe.cn/fapigw/air/citys";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("pId", "xxx");
URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}
// 基本参数配置
NSString *apiUrl = @"http://apis.juhe.cn/fapigw/air/citys"; // 接口请求URL
NSString *apiKey = @"您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
NSDictionary *requestParams = @{
@"key": apiKey,
@"pId": @"xxx",
};
// 发起接口网络请求
NSURLComponents *components = [NSURLComponents componentsWithString:apiUrl];
NSMutableArray *queryItems = [NSMutableArray array];
[requestParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}];
components.queryItems = queryItems;
NSURL *url = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// 网络请求异常处理
NSLog(@"请求异常");
} else {
NSError *jsonError;
NSDictionary *responseResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError) {
// 网络请求成功处理
// NSLog(@"%@", [responseResult objectForKey:@"error_code"]);
NSLog(@"%@", responseResult);
} else {
// 解析结果异常处理
NSLog(@"解析结果异常");
}
}
}];
[task resume];
返回参数说明:
名称 | 类型 | 说明 | |
---|---|---|---|
error_code | int | 返回码 | |
reason | string | 返回结果描述 | |
result | array | 返回结果集 | |
Id | string | 城市地区ID | |
CityName | string | 城市名称 | |
CityCode | string | 城市代码 | |
CityJC | string | 城市简码 |
JSON返回示例:JSON在线格式化工具 >
{
"reason": "success",
"result": [
{
"Id": "1",
"CityName": "北京市",
"CityCode": "110000",
"CityJC": "BJS"
}
],
"error_code": 0
}
请求Header:
名称 | 值 | |
---|---|---|
Content-Type | application/x-www-form-urlencoded |
请求参数说明:
名称 | 必填 | 类型 | 说明 | |
---|---|---|---|---|
key | 是 | string | 接口请求key,在个人中心->数据中心->我的API进行查看 | |
cityId | 是 | string | 需要查询的城市Id |
请求代码示例:
curl -k -i "http://apis.juhe.cn/fapigw/air/live?key=key&cityId=xxx"
<?php
/**
* 1908-查询城市实时空气质量 - 代码参考(根据实际业务情况修改)
*/
// 基本参数配置
$apiUrl = "http://apis.juhe.cn/fapigw/air/live"; // 接口请求URL
$method = "GET"; // 接口请求方式
$headers = ["Content-Type: application/x-www-form-urlencoded"]; // 接口请求header
$apiKey = "您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
$requestParams = [
'key' => $apiKey,
'cityId'=> 'xxx',
];
$requestParamsStr = http_build_query($requestParams);
// 发起接口网络请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $apiUrl . '?' . $requestParamsStr);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (1 == strpos("$" . $apiUrl, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$response = curl_exec($curl);
$httpInfo = curl_getinfo($curl);
curl_close($curl);
// 解析响应结果
$responseResult = json_decode($response, true);
if ($responseResult) {
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
var_dump($responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
// var_dump($httpInfo);
var_dump("请求异常");
}
import requests
# 1908-查询城市实时空气质量 - 代码参考(根据实际业务情况修改)
# 基本参数配置
apiUrl = 'http://apis.juhe.cn/fapigw/air/live' # 接口请求URL
apiKey = '您申请的调用APIkey' # 在个人中心->我的数据,接口名称上方查看
# 接口请求入参配置
requestParams = {
'key': apiKey,
'cityId': 'xxx',
}
# 发起接口网络请求
response = requests.get(apiUrl, params=requestParams)
# 解析响应结果
if response.status_code == 200:
responseResult = response.json()
# 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
print(responseResult)
else:
# 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
print('请求异常')
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func main() {
// 基本参数配置
apiUrl := "http://apis.juhe.cn/fapigw/air/live"
apiKey := "您申请的调用APIkey"
// 接口请求入参配置
requestParams := url.Values{}
requestParams.Set("key", apiKey)
requestParams.Set("cityId", "xxx")
// 发起接口网络请求
resp, err := http.Get(apiUrl + "?" + requestParams.Encode())
if err != nil {
fmt.Println("网络请求异常:", err)
return
}
defer resp.Body.Close()
var responseResult map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&responseResult)
if err != nil {
fmt.Println("解析响应结果异常:", err)
return
}
fmt.Println(responseResult)
}
using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Common_API_Test.Test_Demo
{
class Csharp_get
{
static void Main(string[] args)
{
string url = "http://apis.juhe.cn/fapigw/air/live";
string apiKey = "您申请的调用APIkey";
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("key", apiKey);
data.Add( "cityId", "xxx");
using (WebClient client = new WebClient())
{
string fullUrl = url + "?" + string.Join("&", data.Select(x => x.Key + "=" + x.Value));
try
{
string responseContent = client.DownloadString(fullUrl);
dynamic responseData = JsonConvert.DeserializeObject(responseContent);
if (responseData != null)
{
Console.WriteLine("Return Code: " + responseData["error_code"]);
Console.WriteLine("Return Message: " + responseData["reason"]);
}
else
{
Console.WriteLine("json解析异常!");
}
}
catch (Exception)
{
Console.WriteLine("请检查其它错误");
}
}
}
}
}
const axios = require('axios'); // npm install axios
// 基本参数配置
const apiUrl = 'http://apis.juhe.cn/fapigw/air/live'; // 接口请求URL
const apiKey = '您申请的调用APIkey'; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
const requestParams = {
key: apiKey,
cityId: 'xxx',
};
// 发起接口网络请求
axios.get(apiUrl, {params: requestParams})
.then(response => {
// 解析响应结果
if (response.status === 200) {
const responseResult = response.data;
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
console.log(responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
console.log('请求异常');
}
})
.catch(error => {
// 网络请求失败,可以根据实际情况进行处理
console.log('网络请求失败:', error);
});
package cn.juhe.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class JavaGet {
public static void main(String[] args) throws Exception {
String apiKey = "你申请的key";
String apiUrl = "http://apis.juhe.cn/fapigw/air/live";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("cityId", "xxx");
URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}
// 基本参数配置
NSString *apiUrl = @"http://apis.juhe.cn/fapigw/air/live"; // 接口请求URL
NSString *apiKey = @"您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
NSDictionary *requestParams = @{
@"key": apiKey,
@"cityId": @"xxx",
};
// 发起接口网络请求
NSURLComponents *components = [NSURLComponents componentsWithString:apiUrl];
NSMutableArray *queryItems = [NSMutableArray array];
[requestParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}];
components.queryItems = queryItems;
NSURL *url = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// 网络请求异常处理
NSLog(@"请求异常");
} else {
NSError *jsonError;
NSDictionary *responseResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError) {
// 网络请求成功处理
// NSLog(@"%@", [responseResult objectForKey:@"error_code"]);
NSLog(@"%@", responseResult);
} else {
// 解析结果异常处理
NSLog(@"解析结果异常");
}
}
}];
[task resume];
返回参数说明:
名称 | 类型 | 说明 | |
---|---|---|---|
error_code | int | 返回码 | |
reason | string | 返回结果描述 | |
result | object | 返回结果集 | |
Id | string | 城市Id | |
CityName | string | 城市名称 | |
CityCode | string | 城市代码 | |
CityJC | string | 城市简码 | |
CO | string | 一氧化碳,mg/m3 | |
NO2 | string | 二氧化氮,μg/m3 | |
O3 | string | 臭氧,μg/m3 | |
PM10 | string | 可吸入颗粒物,μg/m3 | |
PM2_5 | string | 细颗粒物,μg/m3 | |
SO2 | string | 二氧化硫,μg/m3 | |
AQI | string | 空气质量指数 | |
Quality | string | 空气质量级别 根据AQI指数,分为优(0
~ 50)、良(51 ~ 100)、轻度污染(101 ~ 150)、中度污染(151 ~ 200)、重度污染(201 ~
300)和严重污染(>300)六个级别。
|
|
PrimaryPollutant | string | 首要污染物 | |
Measure | string | 建议措施描述 | |
Unheathful | string | 健康指引描述 | |
Latitude | string | 纬度坐标 | |
Longitude | string | 经度坐标 |
其他使用说明:
JSON返回示例:JSON在线格式化工具 >
{
"reason": "success",
"result": {
"Id": "1",
"CityName": "北京市",
"CityCode": "110000",
"CityJC": "BJS",
"CO": "0.4",
"NO2": "13",
"O3": "80",
"PM10": "27",
"PM2_5": "19",
"SO2": "3",
"AQI": "28",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "40.0689083333333",
"Longitude": "116.396520833333"
},
"error_code": 0
}
请求Header:
名称 | 值 | |
---|---|---|
Content-Type | application/x-www-form-urlencoded |
请求参数说明:
名称 | 必填 | 类型 | 说明 | |
---|---|---|---|---|
key | 是 | string | 接口请求key,在个人中心->数据中心->我的API进行查看 | |
cityId | 是 | string | 需要查询的城市Id |
请求代码示例:
curl -k -i "http://apis.juhe.cn/fapigw/air/stations?key=key&cityId=xxx"
<?php
/**
* 1909-查询站点实时空气质量 - 代码参考(根据实际业务情况修改)
*/
// 基本参数配置
$apiUrl = "http://apis.juhe.cn/fapigw/air/stations"; // 接口请求URL
$method = "GET"; // 接口请求方式
$headers = ["Content-Type: application/x-www-form-urlencoded"]; // 接口请求header
$apiKey = "您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
$requestParams = [
'key' => $apiKey,
'cityId'=> 'xxx',
];
$requestParamsStr = http_build_query($requestParams);
// 发起接口网络请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $apiUrl . '?' . $requestParamsStr);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (1 == strpos("$" . $apiUrl, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$response = curl_exec($curl);
$httpInfo = curl_getinfo($curl);
curl_close($curl);
// 解析响应结果
$responseResult = json_decode($response, true);
if ($responseResult) {
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
var_dump($responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
// var_dump($httpInfo);
var_dump("请求异常");
}
import requests
# 1909-查询站点实时空气质量 - 代码参考(根据实际业务情况修改)
# 基本参数配置
apiUrl = 'http://apis.juhe.cn/fapigw/air/stations' # 接口请求URL
apiKey = '您申请的调用APIkey' # 在个人中心->我的数据,接口名称上方查看
# 接口请求入参配置
requestParams = {
'key': apiKey,
'cityId': 'xxx',
}
# 发起接口网络请求
response = requests.get(apiUrl, params=requestParams)
# 解析响应结果
if response.status_code == 200:
responseResult = response.json()
# 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
print(responseResult)
else:
# 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
print('请求异常')
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func main() {
// 基本参数配置
apiUrl := "http://apis.juhe.cn/fapigw/air/stations"
apiKey := "您申请的调用APIkey"
// 接口请求入参配置
requestParams := url.Values{}
requestParams.Set("key", apiKey)
requestParams.Set("cityId", "xxx")
// 发起接口网络请求
resp, err := http.Get(apiUrl + "?" + requestParams.Encode())
if err != nil {
fmt.Println("网络请求异常:", err)
return
}
defer resp.Body.Close()
var responseResult map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&responseResult)
if err != nil {
fmt.Println("解析响应结果异常:", err)
return
}
fmt.Println(responseResult)
}
using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Common_API_Test.Test_Demo
{
class Csharp_get
{
static void Main(string[] args)
{
string url = "http://apis.juhe.cn/fapigw/air/stations";
string apiKey = "您申请的调用APIkey";
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("key", apiKey);
data.Add( "cityId", "xxx");
using (WebClient client = new WebClient())
{
string fullUrl = url + "?" + string.Join("&", data.Select(x => x.Key + "=" + x.Value));
try
{
string responseContent = client.DownloadString(fullUrl);
dynamic responseData = JsonConvert.DeserializeObject(responseContent);
if (responseData != null)
{
Console.WriteLine("Return Code: " + responseData["error_code"]);
Console.WriteLine("Return Message: " + responseData["reason"]);
}
else
{
Console.WriteLine("json解析异常!");
}
}
catch (Exception)
{
Console.WriteLine("请检查其它错误");
}
}
}
}
}
const axios = require('axios'); // npm install axios
// 基本参数配置
const apiUrl = 'http://apis.juhe.cn/fapigw/air/stations'; // 接口请求URL
const apiKey = '您申请的调用APIkey'; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
const requestParams = {
key: apiKey,
cityId: 'xxx',
};
// 发起接口网络请求
axios.get(apiUrl, {params: requestParams})
.then(response => {
// 解析响应结果
if (response.status === 200) {
const responseResult = response.data;
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
console.log(responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
console.log('请求异常');
}
})
.catch(error => {
// 网络请求失败,可以根据实际情况进行处理
console.log('网络请求失败:', error);
});
package cn.juhe.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class JavaGet {
public static void main(String[] args) throws Exception {
String apiKey = "你申请的key";
String apiUrl = "http://apis.juhe.cn/fapigw/air/stations";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("cityId", "xxx");
URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}
// 基本参数配置
NSString *apiUrl = @"http://apis.juhe.cn/fapigw/air/stations"; // 接口请求URL
NSString *apiKey = @"您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
NSDictionary *requestParams = @{
@"key": apiKey,
@"cityId": @"xxx",
};
// 发起接口网络请求
NSURLComponents *components = [NSURLComponents componentsWithString:apiUrl];
NSMutableArray *queryItems = [NSMutableArray array];
[requestParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}];
components.queryItems = queryItems;
NSURL *url = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// 网络请求异常处理
NSLog(@"请求异常");
} else {
NSError *jsonError;
NSDictionary *responseResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError) {
// 网络请求成功处理
// NSLog(@"%@", [responseResult objectForKey:@"error_code"]);
NSLog(@"%@", responseResult);
} else {
// 解析结果异常处理
NSLog(@"解析结果异常");
}
}
}];
[task resume];
返回参数说明:
名称 | 类型 | 说明 | |
---|---|---|---|
error_code | int | 返回码 | |
reason | string | 返回结果描述 | |
result | array | 返回结果集 | |
Id | string | 城市Id | |
CityName | string | 城市名称 | |
CityCode | string | 城市代码 | |
CityJC | string | 城市简码 | |
StationName | string | 空气质量监测站点名称 | |
StationCode | string | 空气质量监测站点代码 | |
CO | string | 一氧化碳,mg/m3 | |
NO2 | string | 二氧化氮,μg/m3 | |
O3 | string | 臭氧,μg/m3 | |
PM10 | string | 可吸入颗粒物,μg/m3 | |
PM2_5 | string | 细颗粒物,μg/m3 | |
SO2 | string | 二氧化硫,μg/m3 | |
AQI | string | 空气质量指数 | |
Quality | string | 空气质量级别 根据AQI指数,分为优(0
~ 50)、良(51 ~ 100)、轻度污染(101 ~ 150)、中度污染(151 ~ 200)、重度污染(201 ~
300)和严重污染(>300)六个级别。
|
|
PrimaryPollutant | string | 首要污染物 | |
Measure | string | 建议措施描述 | |
Unheathful | string | 健康指引描述 | |
Latitude | string | 纬度坐标 | |
Longitude | string | 经度坐标 |
其他使用说明:
JSON返回示例:JSON在线格式化工具 >
{
"reason": "success",
"result": [
{
"Id": "417",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "上方山",
"StationCode": "1160A",
"CO": "0.4",
"NO2": "5",
"O3": "83",
"PM10": "—",
"PM2_5": "15",
"SO2": "8",
"AQI": "26",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.2472",
"Longitude": "120.561"
},
{
"Id": "418",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "彩香",
"StationCode": "1162A",
"CO": "0.6",
"NO2": "2",
"O3": "82",
"PM10": "14",
"PM2_5": "<3",
"SO2": "6",
"AQI": "26",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.3019",
"Longitude": "120.591"
},
{
"Id": "419",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "轧钢厂",
"StationCode": "1163A",
"CO": "0.5",
"NO2": "8",
"O3": "96",
"PM10": "7",
"PM2_5": "<3",
"SO2": "5",
"AQI": "30",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.3264",
"Longitude": "120.596"
},
{
"Id": "420",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "吴中区",
"StationCode": "1164A",
"CO": "0.6",
"NO2": "5",
"O3": "101",
"PM10": "7",
"PM2_5": "<3",
"SO2": "7",
"AQI": "32",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.2703",
"Longitude": "120.613"
},
{
"Id": "421",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "苏州新区",
"StationCode": "1165A",
"CO": "0.5",
"NO2": "5",
"O3": "98",
"PM10": "9",
"PM2_5": "7",
"SO2": "8",
"AQI": "31",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.2994",
"Longitude": "120.543"
},
{
"Id": "422",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "苏州工业园区",
"StationCode": "1166A",
"CO": "0.4",
"NO2": "3",
"O3": "86",
"PM10": "17",
"PM2_5": "6",
"SO2": "8",
"AQI": "27",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.3097",
"Longitude": "120.669"
},
{
"Id": "423",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "相城区",
"StationCode": "1167A",
"CO": "0.7",
"NO2": "1",
"O3": "93",
"PM10": "8",
"PM2_5": "<3",
"SO2": "5",
"AQI": "30",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.3708",
"Longitude": "120.641"
},
{
"Id": "424",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "方洲路",
"StationCode": "3289A",
"CO": "0.7",
"NO2": "2",
"O3": "107",
"PM10": "8",
"PM2_5": "<3",
"SO2": "8",
"AQI": "34",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.3188",
"Longitude": "120.743"
},
{
"Id": "425",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "木渎中学",
"StationCode": "3290A",
"CO": "0.6",
"NO2": "5",
"O3": "96",
"PM10": "<6",
"PM2_5": "<3",
"SO2": "6",
"AQI": "30",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.2774",
"Longitude": "120.5031"
},
{
"Id": "426",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "越秀幼儿园",
"StationCode": "3425A",
"CO": "0.7",
"NO2": "6",
"O3": "98",
"PM10": "—",
"PM2_5": "—",
"SO2": "6",
"AQI": "—",
"Quality": "—",
"Measure": "—",
"Unheathful": "—",
"Latitude": "31.1733",
"Longitude": "120.624"
},
{
"Id": "427",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"StationName": "高铁新城",
"StationCode": "3431A",
"CO": "0.6",
"NO2": "16",
"O3": "90",
"PM10": "9",
"PM2_5": "<3",
"SO2": "7",
"AQI": "29",
"Quality": "优",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染",
"Latitude": "31.416",
"Longitude": "120.6505"
}
],
"error_code": 0
}
请求Header:
名称 | 值 | |
---|---|---|
Content-Type | application/x-www-form-urlencoded |
请求参数说明:
名称 | 必填 | 类型 | 说明 | |
---|---|---|---|---|
key | 是 | string | 接口请求key,在个人中心->数据中心->我的API进行查看 | |
cityId | 是 | string | 需要查询的城市Id |
请求代码示例:
curl -k -i "http://apis.juhe.cn/fapigw/air/history?key=key&cityId=xxx"
<?php
/**
* 1921-查询近14天空气质量 - 代码参考(根据实际业务情况修改)
*/
// 基本参数配置
$apiUrl = "http://apis.juhe.cn/fapigw/air/history"; // 接口请求URL
$method = "GET"; // 接口请求方式
$headers = ["Content-Type: application/x-www-form-urlencoded"]; // 接口请求header
$apiKey = "您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
$requestParams = [
'key' => $apiKey,
'cityId'=> 'xxx',
];
$requestParamsStr = http_build_query($requestParams);
// 发起接口网络请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $apiUrl . '?' . $requestParamsStr);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (1 == strpos("$" . $apiUrl, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$response = curl_exec($curl);
$httpInfo = curl_getinfo($curl);
curl_close($curl);
// 解析响应结果
$responseResult = json_decode($response, true);
if ($responseResult) {
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
var_dump($responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
// var_dump($httpInfo);
var_dump("请求异常");
}
import requests
# 1921-查询近14天空气质量 - 代码参考(根据实际业务情况修改)
# 基本参数配置
apiUrl = 'http://apis.juhe.cn/fapigw/air/history' # 接口请求URL
apiKey = '您申请的调用APIkey' # 在个人中心->我的数据,接口名称上方查看
# 接口请求入参配置
requestParams = {
'key': apiKey,
'cityId': 'xxx',
}
# 发起接口网络请求
response = requests.get(apiUrl, params=requestParams)
# 解析响应结果
if response.status_code == 200:
responseResult = response.json()
# 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
print(responseResult)
else:
# 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
print('请求异常')
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func main() {
// 基本参数配置
apiUrl := "http://apis.juhe.cn/fapigw/air/history"
apiKey := "您申请的调用APIkey"
// 接口请求入参配置
requestParams := url.Values{}
requestParams.Set("key", apiKey)
requestParams.Set("cityId", "xxx")
// 发起接口网络请求
resp, err := http.Get(apiUrl + "?" + requestParams.Encode())
if err != nil {
fmt.Println("网络请求异常:", err)
return
}
defer resp.Body.Close()
var responseResult map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&responseResult)
if err != nil {
fmt.Println("解析响应结果异常:", err)
return
}
fmt.Println(responseResult)
}
using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Common_API_Test.Test_Demo
{
class Csharp_get
{
static void Main(string[] args)
{
string url = "http://apis.juhe.cn/fapigw/air/history";
string apiKey = "您申请的调用APIkey";
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("key", apiKey);
data.Add( "cityId", "xxx");
using (WebClient client = new WebClient())
{
string fullUrl = url + "?" + string.Join("&", data.Select(x => x.Key + "=" + x.Value));
try
{
string responseContent = client.DownloadString(fullUrl);
dynamic responseData = JsonConvert.DeserializeObject(responseContent);
if (responseData != null)
{
Console.WriteLine("Return Code: " + responseData["error_code"]);
Console.WriteLine("Return Message: " + responseData["reason"]);
}
else
{
Console.WriteLine("json解析异常!");
}
}
catch (Exception)
{
Console.WriteLine("请检查其它错误");
}
}
}
}
}
const axios = require('axios'); // npm install axios
// 基本参数配置
const apiUrl = 'http://apis.juhe.cn/fapigw/air/history'; // 接口请求URL
const apiKey = '您申请的调用APIkey'; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
const requestParams = {
key: apiKey,
cityId: 'xxx',
};
// 发起接口网络请求
axios.get(apiUrl, {params: requestParams})
.then(response => {
// 解析响应结果
if (response.status === 200) {
const responseResult = response.data;
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
console.log(responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
console.log('请求异常');
}
})
.catch(error => {
// 网络请求失败,可以根据实际情况进行处理
console.log('网络请求失败:', error);
});
package cn.juhe.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class JavaGet {
public static void main(String[] args) throws Exception {
String apiKey = "你申请的key";
String apiUrl = "http://apis.juhe.cn/fapigw/air/history";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("cityId", "xxx");
URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}
// 基本参数配置
NSString *apiUrl = @"http://apis.juhe.cn/fapigw/air/history"; // 接口请求URL
NSString *apiKey = @"您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
NSDictionary *requestParams = @{
@"key": apiKey,
@"cityId": @"xxx",
};
// 发起接口网络请求
NSURLComponents *components = [NSURLComponents componentsWithString:apiUrl];
NSMutableArray *queryItems = [NSMutableArray array];
[requestParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}];
components.queryItems = queryItems;
NSURL *url = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// 网络请求异常处理
NSLog(@"请求异常");
} else {
NSError *jsonError;
NSDictionary *responseResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError) {
// 网络请求成功处理
// NSLog(@"%@", [responseResult objectForKey:@"error_code"]);
NSLog(@"%@", responseResult);
} else {
// 解析结果异常处理
NSLog(@"解析结果异常");
}
}
}];
[task resume];
返回参数说明:
名称 | 类型 | 说明 | |
---|---|---|---|
error_code | int | 返回码 | |
reason | string | 返回结果描述 | |
result | object | 返回结果集 | |
- | - | - | |
CityInfo | object | 城市基本信息 | |
Id | string | 城市Id | |
CityName | string | 城市名称 | |
CityCode | string | 城市代码 | |
CityJC | string | 城市简码 | |
Latitude | string | 纬度坐标 | |
Longitude | string | 经度坐标 | |
- | - | - | |
Data | array | 过去14天数据 | |
Date | string | 日期 | |
CO_24h | string | 24小时平均 一氧化碳,mg/m3 | |
NO2_24h | string | 24小时平均 二氧化氮,mg/m3 | |
O3_8h_24h | string | 日8小时最大平均 臭氧,mg/m3 | |
PM10_24h | string | 24小时平均 可吸入颗粒物氮,mg/m3 | |
PM2_5_24h | string | 24小时平均 细颗粒物,mg/m3 | |
SO2_24h | string | 24小时平均 二氧化硫,mg/m3 | |
AQI | string | 空气质量指数 | |
Quality | string | 空气质量级别 根据AQI指数,分为优(0
~ 50)、良(51 ~ 100)、轻度污染(101 ~ 150)、中度污染(151 ~ 200)、重度污染(201 ~
300)和严重污染(>300)六个级别。
|
|
PrimaryPollutant | string | 首要污染物 | |
Measure | string | 建议措施描述 | |
Unheathful | string | 健康指引描述 |
其他使用说明:
JSON返回示例:JSON在线格式化工具 >
{
"reason": "success",
"result": {
"cityInfo": {
"Id": "73",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"Latitude": "31.3010181818182",
"Longitude": "120.612236363636"
},
"data": [
{
"Date": "2024-07-31",
"CO_24h": "0.5",
"NO2_24h": "11",
"O3_8h_24h": "141",
"PM10_24h": "26",
"PM2_5_24h": "15",
"SO2_24h": "8",
"AQI": "85",
"PrimaryPollutant": "臭氧8小时",
"Measure": "极少数异常敏感人群应减少户外活动",
"Unheathful": "空气质量可接受,但某些污染物可能对极少数异常敏感人群健康有较弱影响"
},
{
"Date": "2024-07-30",
"CO_24h": "0.5",
"NO2_24h": "11",
"O3_8h_24h": "121",
"PM10_24h": "22",
"PM2_5_24h": "10",
"SO2_24h": "8",
"AQI": "68",
"PrimaryPollutant": "臭氧8小时",
"Measure": "极少数异常敏感人群应减少户外活动",
"Unheathful": "空气质量可接受,但某些污染物可能对极少数异常敏感人群健康有较弱影响"
}
]
},
"error_code": 0
}
请求Header:
名称 | 值 | |
---|---|---|
Content-Type | application/x-www-form-urlencoded |
请求参数说明:
名称 | 必填 | 类型 | 说明 | |
---|---|---|---|---|
key | 是 | string | 接口请求key,在个人中心->数据中心->我的API进行查看 | |
cityId | 是 | string | 需要查询的城市Id |
请求代码示例:
curl -k -i "http://apis.juhe.cn/fapigw/air/historyHours?key=key&cityId=xxx"
<?php
/**
* 1922-查询近24小时空气质量 - 代码参考(根据实际业务情况修改)
*/
// 基本参数配置
$apiUrl = "http://apis.juhe.cn/fapigw/air/historyHours"; // 接口请求URL
$method = "GET"; // 接口请求方式
$headers = ["Content-Type: application/x-www-form-urlencoded"]; // 接口请求header
$apiKey = "您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
$requestParams = [
'key' => $apiKey,
'cityId'=> 'xxx',
];
$requestParamsStr = http_build_query($requestParams);
// 发起接口网络请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $apiUrl . '?' . $requestParamsStr);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (1 == strpos("$" . $apiUrl, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$response = curl_exec($curl);
$httpInfo = curl_getinfo($curl);
curl_close($curl);
// 解析响应结果
$responseResult = json_decode($response, true);
if ($responseResult) {
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
var_dump($responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
// var_dump($httpInfo);
var_dump("请求异常");
}
import requests
# 1922-查询近24小时空气质量 - 代码参考(根据实际业务情况修改)
# 基本参数配置
apiUrl = 'http://apis.juhe.cn/fapigw/air/historyHours' # 接口请求URL
apiKey = '您申请的调用APIkey' # 在个人中心->我的数据,接口名称上方查看
# 接口请求入参配置
requestParams = {
'key': apiKey,
'cityId': 'xxx',
}
# 发起接口网络请求
response = requests.get(apiUrl, params=requestParams)
# 解析响应结果
if response.status_code == 200:
responseResult = response.json()
# 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
print(responseResult)
else:
# 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
print('请求异常')
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func main() {
// 基本参数配置
apiUrl := "http://apis.juhe.cn/fapigw/air/historyHours"
apiKey := "您申请的调用APIkey"
// 接口请求入参配置
requestParams := url.Values{}
requestParams.Set("key", apiKey)
requestParams.Set("cityId", "xxx")
// 发起接口网络请求
resp, err := http.Get(apiUrl + "?" + requestParams.Encode())
if err != nil {
fmt.Println("网络请求异常:", err)
return
}
defer resp.Body.Close()
var responseResult map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&responseResult)
if err != nil {
fmt.Println("解析响应结果异常:", err)
return
}
fmt.Println(responseResult)
}
using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Common_API_Test.Test_Demo
{
class Csharp_get
{
static void Main(string[] args)
{
string url = "http://apis.juhe.cn/fapigw/air/historyHours";
string apiKey = "您申请的调用APIkey";
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("key", apiKey);
data.Add( "cityId", "xxx");
using (WebClient client = new WebClient())
{
string fullUrl = url + "?" + string.Join("&", data.Select(x => x.Key + "=" + x.Value));
try
{
string responseContent = client.DownloadString(fullUrl);
dynamic responseData = JsonConvert.DeserializeObject(responseContent);
if (responseData != null)
{
Console.WriteLine("Return Code: " + responseData["error_code"]);
Console.WriteLine("Return Message: " + responseData["reason"]);
}
else
{
Console.WriteLine("json解析异常!");
}
}
catch (Exception)
{
Console.WriteLine("请检查其它错误");
}
}
}
}
}
const axios = require('axios'); // npm install axios
// 基本参数配置
const apiUrl = 'http://apis.juhe.cn/fapigw/air/historyHours'; // 接口请求URL
const apiKey = '您申请的调用APIkey'; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
const requestParams = {
key: apiKey,
cityId: 'xxx',
};
// 发起接口网络请求
axios.get(apiUrl, {params: requestParams})
.then(response => {
// 解析响应结果
if (response.status === 200) {
const responseResult = response.data;
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
console.log(responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
console.log('请求异常');
}
})
.catch(error => {
// 网络请求失败,可以根据实际情况进行处理
console.log('网络请求失败:', error);
});
package cn.juhe.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class JavaGet {
public static void main(String[] args) throws Exception {
String apiKey = "你申请的key";
String apiUrl = "http://apis.juhe.cn/fapigw/air/historyHours";
HashMap<String, String> map = new HashMap<>();
map.put("key", apiKey);
map.put("cityId", "xxx");
URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
}
public static String params(Map<String, String> map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}
// 基本参数配置
NSString *apiUrl = @"http://apis.juhe.cn/fapigw/air/historyHours"; // 接口请求URL
NSString *apiKey = @"您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
NSDictionary *requestParams = @{
@"key": apiKey,
@"cityId": @"xxx",
};
// 发起接口网络请求
NSURLComponents *components = [NSURLComponents componentsWithString:apiUrl];
NSMutableArray *queryItems = [NSMutableArray array];
[requestParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}];
components.queryItems = queryItems;
NSURL *url = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// 网络请求异常处理
NSLog(@"请求异常");
} else {
NSError *jsonError;
NSDictionary *responseResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError) {
// 网络请求成功处理
// NSLog(@"%@", [responseResult objectForKey:@"error_code"]);
NSLog(@"%@", responseResult);
} else {
// 解析结果异常处理
NSLog(@"解析结果异常");
}
}
}];
[task resume];
返回参数说明:
名称 | 类型 | 说明 | |
---|---|---|---|
error_code | int | 返回码 | |
reason | string | 返回结果描述 | |
result | object | 返回结果集 | |
- | - | - | |
CityInfo | object | 城市基本信息 | |
Id | string | 城市Id | |
CityName | string | 城市名称 | |
CityCode | string | 城市代码 | |
CityJC | string | 城市简码 | |
Latitude | string | 纬度坐标 | |
Longitude | string | 经度坐标 | |
- | - | - | |
Data | array | 过去24小时数据 | |
Time | string | 小时 | |
CO | string | 一氧化碳,mg/m3 | |
NO2 | string | 二氧化氮,μg/m3 | |
O3 | string | 臭氧,μg/m3 | |
PM10 | string | 可吸入颗粒物,μg/m3 | |
PM2_5 | string | 细颗粒物,μg/m3 | |
SO2 | string | 二氧化硫,μg/m3 | |
AQI | string | 空气质量指数 | |
Quality | string | 空气质量级别 根据AQI指数,分为优(0
~ 50)、良(51 ~ 100)、轻度污染(101 ~ 150)、中度污染(151 ~ 200)、重度污染(201 ~
300)和严重污染(>300)六个级别。
|
|
PrimaryPollutant | string | 首要污染物 | |
Measure | string | 建议措施描述 | |
Unheathful | string | 健康指引描述 |
其他使用说明:
JSON返回示例:JSON在线格式化工具 >
{
"reason": "success",
"result": {
"CityInfo": {
"Id": "73",
"CityName": "苏州市",
"CityCode": "320500",
"CityJC": "SZS",
"Latitude": "31.3010181818182",
"Longitude": "120.612236363636"
},
"Data": [
{
"Time": "2024-08-02 10:00:00",
"CO": "0.5",
"NO2": "4",
"O3": "89",
"PM10": "18",
"PM2_5": "9",
"SO2": "8",
"AQI": "28",
"Quality": "优",
"PrimaryPollutant": "—",
"Measure": "各类人群可正常活动",
"Unheathful": "空气质量令人满意,基本无空气污染"
}
]
},
"error_code": 0
}
服务级错误码参照(error_code):
错误码 | 说明 |
---|
系统级错误码参照:
错误码 | 说明 | 旧版本(resultcode) | |
---|---|---|---|
10001 | 错误的请求KEY | 101 | |
10002 | 该KEY无请求权限 | 102 | |
10003 | KEY过期 | 103 | |
10004 | 错误的OPENID | 104 | |
10005 | 应用未审核超时,请提交认证 | 105 | |
10007 | 未知的请求源 | 107 | |
10008 | 被禁止的IP | 108 | |
10009 | 被禁止的KEY | 109 | |
10011 | 当前IP请求超过限制 | 111 | |
10012 | 请求超过次数限制 | 112 | |
10013 | 测试KEY超过请求限制 | 113 | |
10014 | 系统内部异常 (调用充值类业务时,请务必联系客服或通过订单查询接口检测订单,避免造成损失) | 114 | |
10020 | 接口维护 | 120 | |
10021 | 接口停用 | 121 |
错误码格式说明(示例:200201):
2 | 002 | 01 | |
---|---|---|---|
服务级错误(1为系统级错误) | 服务模块代码(即数据ID) | 具体错误代码 |
限企业实名用户使用
本接口需提供应用场景审核
个人(基础认证)实名用户无法使用