美文网首页
天气相关api 心知天气API 免费

天气相关api 心知天气API 免费

作者: 王冠CodeForces | 来源:发表于2017-01-01 20:41 被阅读0次

/*

* 天气相关api  心知天气API 免费

* http://apistore.baidu.com/apiworks/servicedetail/2573.html

*/

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

//alfx1c184e433566814aa5bd18abf5930ffdalfx

public class Test1 {

/**

* @param urlAll

*            :请求接口

* @param httpArg

*            :参数

* @return 返回结果

*/

public static String request(String httpUrl, String httpArg) {

BufferedReader reader = null;

String result = null;

StringBuffer sbf = new StringBuffer();

httpUrl = httpUrl + "?" + httpArg;

try {

URL url = new URL(httpUrl);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

connection.setRequestMethod("GET");

// 填入apikey到HTTP header

connection.setRequestProperty("apikey",  "alfx1c184e433566814aa5bd18abf5930ffdalfx");

connection.connect();

InputStream is = connection.getInputStream();

reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

String strRead = null;

while ((strRead = reader.readLine()) != null) {

sbf.append(strRead);

sbf.append("\r\n");

}

reader.close();

result = sbf.toString();

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String httpUrl = "http://apis.baidu.com/thinkpage/weather_api/suggestion";

String httpArg = "location=daqing&language=zh-Hans&unit=c&start=0&days=3";

String jsonResult = request(httpUrl, httpArg);

//System.out.println(jsonResult);

JSONObject ob = new JSONObject();

JSONArray ob1 = (JSONArray) ob.get("results");

JSONObject ob2 = (JSONObject) ob1.get(0);

JSONObject ob3 = (JSONObject) ob2.get("location");

String s = (String) ob3.get("name");

System.out.println(s);

}

}

//{

//    "results": [

//        {

//            "location": {

//                "id": "YB2BVPSH4JM5",

//                "name": "大庆",

//                "country": "CN",

//                "path": "大庆,大庆,黑龙江,中国",

//                "timezone": "Asia/Shanghai",

//                "timezone_offset": "+08:00"

//            },

//            "daily": [

//                {

//                    "date": "2016-12-30",

//                    "text_day": "晴",  白天天气

//                    "code_day": "0",

//                    "text_night": "晴",  夜间天气

//                    "code_night": "0",

//                    "high": "-8",   最高温度

//                    "low": "-18",   最低温度

//                    "precip": "",

//                    "wind_direction": "西",

//                    "wind_direction_degree": "270",

//                    "wind_speed": "15",

//                    "wind_scale": "3"   风力级别

//                },

//                {

//                    "date": "2016-12-31",

//                    "text_day": "晴",

//                    "code_day": "0",

//                    "text_night": "晴",

//                    "code_night": "0",

//                    "high": "-7",

//                    "low": "-18",

//                    "precip": "",

//                    "wind_direction": "西",

//                    "wind_direction_degree": "270",

//                    "wind_speed": "15",

//                    "wind_scale": "3"

//                },

//                {

//                    "date": "2017-01-01",

//                    "text_day": "晴",

//                    "code_day": "0",

//                    "text_night": "晴",

//                    "code_night": "0",

//                    "high": "-10",

//                    "low": "-17",

//                    "precip": "",

//                    "wind_direction": "南",

//                    "wind_direction_degree": "180",

//                    "wind_speed": "15",

//                    "wind_scale": "3"

//                }

//            ],

//            "last_update": "2016-12-30T08:00:00+08:00"

//        }

//    ]

//}

相关文章

  • 天气相关api 心知天气API 免费

    /* * 天气相关api 心知天气API 免费 * http://apistore.baidu.com/apiwo...

  • 心知天气数据 API 产品的高并发实践

    心知天气数据 API 产品的高并发实践 心知天气作为国内领先的商业气象服务提供商,天气数据 API 产品从公司创立...

  • 心知天气接口 API 文档

    API的方法:GET API的URL:https://api.seniverse.com/v3/weather/n...

  • 天气api

    JSONhttp://wthrcdn.etouch.cn/weather_mini?city=北京通过城市名字获得...

  • 天气API

    天气 (天气网):http://www.weather.com.cn/data/sk/101010100.html...

  • 天气API

    免费的天气api, 支持iframe引用 http://www.tianqiapi.com/ 最新城市ID列表 h...

  • 天气api

    前段时间应项目需求,需要做天气展示,个人就找了很多的天气api,比如国家天气网、墨迹天气、彩云天气等等(不是打广告...

  • 利用JSONP跨域获取天气数据

    免费接口很少有一直比较稳定的,经过简单调查,这里选用了心知天气API,免费版:400次/小时,也就是9600次/天...

  • vue中的实时天气

    网址 ↓ 右上“api文档” -- 左下“免费接口” -- “免费时况天气” 免费实况天气接口(测试需登录) 百度...

  • Postman的工作原理

    第一个请求: 免费天气api:http://t.weather.sojson.com/api/weather/ci...

网友评论

      本文标题:天气相关api 心知天气API 免费

      本文链接:https://www.haomeiwen.com/subject/jjmivttx.html