/*
* 天氣相關(guān)api ?心知天氣API 免費(fèi)
* 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
* ? ? ? ? ? ?:請(qǐng)求接口
* @param httpArg
* ? ? ? ? ? ?:參數(shù)
* @return 返回結(jié)果
*/
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": "大慶,大慶,黑龍江,中國(guó)",
// ? ? ? ? ? ? ? ?"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" ? 風(fēng)力級(jí)別
// ? ? ? ? ? ? ? ?},
// ? ? ? ? ? ? ? ?{
// ? ? ? ? ? ? ? ? ? ?"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"
// ? ? ? ?}
// ? ?]
//}