概述:

本节讲述日常生活中常用的一些公众服务的查询接口,其中包括天气查询,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



posted on 2015-03-26 11:49  LZU-GIS  阅读(4465)  评论(0编辑  收藏  举报