天氣相關api 心知天氣API 免費

/*

* 天氣相關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

* ? ? ? ? ? ?:參數(shù)

* @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"

// ? ? ? ?}

// ? ?]

//}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容