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

2017-01-01  本文已影响0人  王冠CodeForces

/*

* 天气相关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"

//        }

//    ]

//}

上一篇 下一篇

猜你喜欢

热点阅读