超好⽤的PM2.5空⽓质量指数API接⼝Java调⽤
空⽓质量指数
⽀持全国367个城市空⽓质量指数,提供实时空⽓质量情况,每⼩时更新,服务⽀持功能包括:实时查询空⽓质量,⼩时粒度;实时给出空⽓质量级别和⾸要污染物等
. 获取全国PM2.5指数排⾏榜
复制代码
. 查询制定城市PM2.5空⽓质量
复制代码
1、获取全国PM2.5指数排⾏榜
package net.ller;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
HttpURLConnection;
MalformedURLException;
URL;
URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
/**
* httpUrlConnection访问远程接⼝⼯具
*/
public class Api
{
/**
* ⽅法体说明:向远程接⼝发起请求,返回字节流类型结果
* param url 接⼝地址
* param requestMethod 请求⽅式
* param params 传递参数重点:参数值需要⽤Ba64进⾏转码
* return InputStream 返回结果
*/
public static InputStream httpRequestToStream(String url, String requestMethod, Map<String, String> params)
{
InputStream is = ;
try
{
String parameters = "";
boolean hasParams = fal;
// 将参数集合拼接成特定格式,如name=zhangsan&age=24
for (String key : params.keySet())
{
String value = (key), "UTF-8");
parameters += key + "=" + value + "&";
hasParams = true;
}
if (hasParams)
{
parameters = parameters.substring(0, parameters.length() - 1);
}
// 请求⽅式是否为get
boolean isGet = "get".equalsIgnoreCa(requestMethod);
// 请求⽅式是否为post
boolean isPost = "post".equalsIgnoreCa(requestMethod);
if (isGet)
{
url += "?" + parameters;
}
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
// 请求的参数类型(使⽤restlet框架时,为了兼容框架,必须设置Content-Type为“”空)
conn.tRequestProperty("Content-Type", "application/octet-stream");
// conn.tRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 设置连接超时时间
conn.tConnectTimeout(50000);
// 设置读取返回内容超时时间
conn.tReadTimeout(50000);
// 设置向HttpURLConnection对象中输出,因为post⽅式将请求参数放在http正⽂内,因此需要设置为ture,默认fal if (isPost)
{
conn.tDoOutput(true);
}
// 设置从HttpURLConnection对象读⼊,默认为true
conn.tDoInput(true);
// 设置是否使⽤缓存,post⽅式不能使⽤缓存
if (isPost)
{
conn.tUCaches(fal);
}
// 设置请求⽅式,默认为GET
conn.tRequestMethod(requestMethod);
// post⽅式需要将传递的参数输出到conn对象中
if (isPost)
{
DataOutputStream dos = new OutputStream());
dos.writeBytes(parameters);
dos.flush();
dos.clo();
}
/
/ 从HttpURLConnection对象中读取响应的消息
// 执⾏该语句时才正式发起请求
is = InputStream();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return is;
}
public static void main(String args[])
{
String url = "/common/postcode/getPostCodeByAddr";
String url = "/common/postcode/getPostCodeByAddr"; String requestMethod = "POST";
Map<String, String> params = new HashMap<String, String>();
String result = ;
try
{
InputStream is = httpRequestToStream(url, requestMethod, params);
byte[] b = new byte[is.available()];
result = new String(b);
}
catch(IOException e)
{
e.printStackTrace();
}
if (result != )
{
JSONObject jsonObject = JSONObject.parObject(result);
String status_code = String("statusCode");
if (status_code == "000000")
{
// 状态码为000000, 说明请求成功
System.out.println("请求成功:" + String("result"));
}
el
{
// 状态码⾮000000, 说明请求失败
System.out.println("请求失败:" + String("desc"));
}
}
el
{
// 返回内容异常,发送请求失败,以下可根据业务逻辑⾃⾏修改
System.out.println("发送请求失败");
}
}
}
复制代码
2、查询制定城市PM2.5空⽓质量
package net.ller;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
HttpURLConnection;
MalformedURLException;
URL;
URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
/**
* httpUrlConnection访问远程接⼝⼯具
*/
public class Api
{
/**
* ⽅法体说明:向远程接⼝发起请求,返回字节流类型结果
* param url 接⼝地址
* param requestMethod 请求⽅式
* param requestMethod 请求⽅式
* param params 传递参数重点:参数值需要⽤Ba64进⾏转码
* return InputStream 返回结果
*/
public static InputStream httpRequestToStream(String url, String requestMethod, Map<String, String> params)
{
InputStream is = ;
try
{
String parameters = "";
boolean hasParams = fal;
// 将参数集合拼接成特定格式,如name=zhangsan&age=24
for (String key : params.keySet())
{
String value = (key), "UTF-8");
parameters += key + "=" + value + "&";
hasParams = true;
}
if (hasParams)
{
parameters = parameters.substring(0, parameters.length() - 1);
}
// 请求⽅式是否为get
boolean isGet = "get".equalsIgnoreCa(requestMethod);
// 请求⽅式是否为post
boolean isPost = "post".equalsIgnoreCa(requestMethod);
if (isGet)
{
url += "?" + parameters;
}
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
// 请求的参数类型(使⽤restlet框架时,为了兼容框架,必须设置Content-Type为“”空)
conn.tRequestProperty("Content-Type", "application/octet-stream");
// conn.tRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 设置连接超时时间
conn.tConnectTimeout(50000);
// 设置读取返回内容超时时间
conn.tReadTimeout(50000);
// 设置向HttpURLConnection对象中输出,因为post⽅式将请求参数放在http正⽂内,因此需要设置为ture,默认fal if (isPost)
{
conn.tDoOutput(true);
}
// 设置从HttpURLConnection对象读⼊,默认为true
conn.tDoInput(true);
// 设置是否使⽤缓存,post⽅式不能使⽤缓存
if (isPost)
{
conn.tUCaches(fal);
}
// 设置请求⽅式,默认为GET
conn.tRequestMethod(requestMethod);
/
/ post⽅式需要将传递的参数输出到conn对象中
if (isPost)
{
DataOutputStream dos = new OutputStream());
dos.writeBytes(parameters);
dos.flush();
dos.clo();
}
// 从HttpURLConnection对象中读取响应的消息
// 执⾏该语句时才正式发起请求
is = InputStream();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return is;
}
public static void main(String args[])
{
String url = "/common/postcode/getPostCodeByAddr"; String requestMethod = "POST";
Map<String, String> params = new HashMap<String, String>();
params.put("city", ""); //城市,如“⼴州市”
String result = ;
try
{
InputStream is = httpRequestToStream(url, requestMethod, params);
byte[] b = new byte[is.available()];
result = new String(b);
}
catch(IOException e)
{
e.printStackTrace();
}
if (result != )
{
JSONObject jsonObject = JSONObject.parObject(result);
String status_code = String("statusCode");
if (status_code == "000000")
{
// 状态码为000000, 说明请求成功
System.out.println("请求成功:" + String("result"));
}
el
{
// 状态码⾮000000, 说明请求失败
System.out.println("请求失败:" + String("desc"));
}
}
el
{
// 返回内容异常,发送请求失败,以下可根据业务逻辑⾃⾏修改
System.out.println("发送请求失败");
}
}
}
复制代码