概述:
本節講述日常生活中常用的一些公眾服務的查詢接口,其中包括天氣查詢,pm2.5情況查詢,身份證查詢、電話歸屬地查詢,IP來源於地址查詢等。
查詢接口:
一、天氣查詢
a、提供以下天氣信息:
1. 當前時間
2. 當前氣溫
3. 最高氣溫
4. 最低氣溫
5. 天氣情況,晴,多雲等
6. 風向
7. 風速
8. 經緯度、海拔
9. 日出日落時間
b、提供接口
1. 天氣查詢——根據城市代碼
接口地址:http://apistore.baidu.com/microservice/weather
請求方法:GET
| 參數名 | 類型 | 必填 | 參數位置 | 描述 | 默認值 |
|---|---|---|---|---|---|
| cityid | string | 是 | urlParam | 城市代碼 | 101010100 |
http://apistore.baidu.com/microservice/weather?cityid=101010100
2. 天氣查詢——根據城市名稱
接口地址:http://apistore.baidu.com/microservice/weather
請求方法:GET
| 參數名 | 類型 | 必填 | 參數位置 | 描述 | 默認值 |
|---|---|---|---|---|---|
| cityname | string | 是 | urlParam | 城市名稱 | 北京 |
http://apistore.baidu.com/microservice/weather?cityname=北京
3. 天氣查詢——根據城市拼音
接口地址:http://apistore.baidu.com/microservice/weather
請求方法:GET
| 參數名 | 類型 | 必填 | 參數位置 | 描述 | 默認值 |
|---|---|---|---|---|---|
| citypinyin | string | 是 | urlParam | 城市拼音 | beijing |
http://apistore.baidu.com/microservice/weather?citypinyin=beijing
天氣查詢返回的數據如下:
JSON返回示例:
{
errNum: 0,
errMsg: "success",
retData: {
city: "北京", //城市
pinyin: "beijing", //城市拼音
citycode: "101010100", //城市編碼
date: "15-02-11", //日期
time: "11:00", //發布時間
postCode: "100000", //郵編
longitude: 116.391, //經度
latitude: 39.904, //維度
altitude: "33", //海拔
weather: "晴", //天氣情況
temp: "10", //氣溫
l_tmp: "-4", //最低氣溫
h_tmp: "10", //最高氣溫
WD: "無持續風向", //風向
WS: "微風(<10m/h)", //風力
sunrise: "07:12", //日出時間
sunset: "17:44" //日落時間
}
}
4. 城市信息查詢——根據城市名稱
接口地址:http://apistore.baidu.com/microservice/cityinfo
請求方法:GET
| 參數名 | 類型 | 必填 | 參數位置 | 描述 | 默認值 |
|---|---|---|---|---|---|
| cityname | string | 是 | urlParam | 城市名稱 | 北京 |
http://apistore.baidu.com/microservice/cityinfo?cityname=北京
JSON返回示例:
{
errNum: 0,
retMsg: "success",
retData: {
cityName: "北京",
provinceName: "北京",
cityCode: "101010100", //天氣預報城市代碼
zipCode: "100000", //郵編
telAreaCode: "010" //電話區號
}
}
二、PM2.5查詢
a、空氣質量指數服務支持以下功能:
1、實時查詢空氣質量,小時粒度;
2、實時給出空氣質量AQI指數,並給出空氣質量級別和首要污染物;
b、查詢接口
接口地址:http://apistore.baidu.com/microservice/aqi
請求方法:GET
| 參數名 | 類型 | 必填 | 參數位置 | 描述 | 默認值 |
|---|---|---|---|---|---|
| city | string | 是 | urlParam | 城市名,具體支持城市請查看基本介紹 | 北京 |
http://apistore.baidu.com/microservice/aqi?city=北京
JSON返回示例:
{
errNum: 0, //非0表示失敗
retMsg: "success",
retData: {
city: "北京", //城市,支持城市列表,請參考基本信息說明
time: "2014-12-04T13:00:00Z", //數據采集時間
aqi: 38, //空氣質量指數
level: "優", //空氣等級
core: "" //首要污染物
}
}
三、身份證信息查詢
a、提供以下功能:
1、身份證歸屬地信息查詢
b、查詢接口
接口地址:http://apistore.baidu.com/microservice/icardinfo
請求方法:GET
| 參數名 | 類型 | 必填 | 參數位置 | 描述 | 默認值 |
|---|---|---|---|---|---|
| id | string | 是 | urlParam | 身份證號 | 420984198704207896 |
http://apistore.baidu.com/microservice/icardinfo?id=420984198704207896
JSON返回示例:
{
"errNum": 0,
"retMsg": "success",
"retData": {
"sex": "M", //M-男,F-女,N-未知
"birthday": "1987-04-20",
"address": "湖北省孝感市漢川市"
}
}
三、電話歸屬地查詢
a、功能
提供手機號碼歸屬地的查詢,獲取號碼在的省份以及對應的運營商
b、調用接口
接口地址:http://apistore.baidu.com/microservice/mobilephone
請求方法:GET
| 參數名 | 類型 | 必填 | 參數位置 | 描述 | 默認值 |
|---|---|---|---|---|---|
| tel | string | 是 | urlParam | 手機號碼 | 15846530170 |
http://apistore.baidu.com/microservice/mobilephone?tel=15846530170
JSON返回示例:
{
errNum: 0,
errMsg: "success",
retData: {
telString: "15846530170", //手機號碼
province: "黑龍江", //省份
carrier: "黑龍江移動" //運營商
}
}
四、IP查詢
a、功能簡介
1. 顯示查詢的IP地址號
2. 顯示IP地址所在的國家
3. 顯示IP地址所在的大的區域,比如華北,華南
4. 顯示IP地址所在的省,市
5. 顯示具體的區
6. 顯示運營商
b、調用接口
接口地址:http://apistore.baidu.com/microservice/iplookup
請求方法:GET
| 參數名 | 類型 | 必填 | 參數位置 | 描述 | 默認值 |
|---|---|---|---|---|---|
| ip | string | 是 | urlParam | ip地址 | 117.89.35.58 |
http://apistore.baidu.com/microservice/iplookup?ip=117.89.35.58
JSON返回示例:
{
"errNum": 0,
"errMsg": "success",
"retData": {
"ip": "117.89.35.58",
"country": "中國",
"province": "江蘇",
"city": "南京",
"district": "鼓樓",
"carrier": "中國電信"
}
}
在java中調用可為:
package com.lzugis.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
public class GetPmInfo {
/**
* @param args
*/
public static void main(String[] args) {
//天氣(根據拼音)
String tqMcUrl = "http://apistore.baidu.com/microservice/weather";
String tqMcParam = "cityname=北京";
String tqMcResult = SendGET(tqMcUrl,tqMcParam);
JSON tqMcJson = JSONObject.fromObject(tqMcResult);
System.out.println(tqMcJson);
//天氣(根據拼音)
String tqPyUrl = "http://apistore.baidu.com/microservice/weather";
String tqPyParam = "citypinyin=beijing";
String tqPyResult = SendGET(tqPyUrl,tqPyParam);
JSON tqPyJson = JSONObject.fromObject(tqPyResult);
System.out.println(tqPyJson);
//pm2.5
String pmUrl = "http://apistore.baidu.com/microservice/aqi";
String pmParam = "city=北京";
String pmResult = SendGET(pmUrl,pmParam);
JSON pmJson = JSONObject.fromObject(pmResult);
System.out.println(pmJson);
}
public static String SendGET(String url,String param){
String result="";//訪問返回結果
BufferedReader read=null;//讀取訪問結果
try {
//創建url
URL realurl=new URL(url+"?"+param);
//打開連接
URLConnection connection=realurl.openConnection();
// 設置通用的請求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//建立連接
connection.connect();
// 獲取所有響應頭字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍歷所有的響應頭字段,獲取到cookies等
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定義 BufferedReader輸入流來讀取URL的響應
read = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;//循環讀取
while ((line = read.readLine()) != null) {
result += line;
}
}
catch (IOException e) {
e.printStackTrace();
}
finally{
if(read!=null){//關閉流
try {
read.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
git@code.csdn.net:snippets/628255.git

