免費節假日api接口使用教程-聚合數據
文章目錄
- 📖訪問官網
- 🌰例子
- 完整代碼
- 🖊?最后總結
📖訪問官網
聚合數據
官網地址 https://dashboard.juhe.cn/home
點擊api
接口文檔
🌰例子
get方式
curl -k -i -d “key=您申請的AppKey&date=2021-05-09” http://apis.juhe.cn/fapig/calendar/day
返回結果
{"reason": "success","result": {"date": "2021-05-09","week": "星期日","statusDesc": "周末","status": null,"animal": "牛","avoid": "訂婚.上梁.納采.蓋屋.開倉","cnDay": "日","day": "9","desc": "母親節","gzDate": "丁巳","gzMonth": "癸巳","gzYear": "辛丑","isBigMonth": "1","lDate": "廿八","lMonth": "三","lunarDate": "28","lunarMonth": "3","lunarYear": "2021","month": "5","suit": "搬家.裝修.開業.結婚.入宅.領證.開工.動土.安床.出行.安葬.開張.作灶.旅游.求嗣.赴任.修造.祈福.祭祀.解除.開市.牧養.納財.納畜.開光.嫁娶.移徙.經絡.立券.求醫.豎柱.栽種.齋醮.求財","term": "","value": "母親節","year": "2021"},"error_code": 0
}
完整代碼
import net.sf.json.JSONObject;import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.HashMap;public class ApiDemo {public static void main(String[] args) {// 發送http請求的urlString url = "http://apis.juhe.cn/fapig/calendar/day";Map<String, String> params = new HashMap<String, String>();params.put("key", "您申請的AppKey"); // 在個人中心->我的數據,接口名稱上方查看params.put("date", "2021-05-09"); // 指定日期,格式為yyyy-MM-dd,如:2021-05-01String paramsStr = urlencode(params);System.out.println(paramsStr);String response = doGet(url,paramsStr);// // post請求// String response = doPost(url,paramsStr);// 輸出請求結果System.out.println(response);try {// 解析請求結果,json:JSONObject jsonObject = JSONObject.fromObject(response);System.out.println(jsonObject);// 具體返回示例值,參考返回參數說明、json返回示例} catch (Exception e) {e.printStackTrace();}}// 將map型轉為請求參數型public static String urlencode(Map<String, String> data) {StringBuilder sb = new StringBuilder();for (Map.Entry i : data.entrySet()) {try {sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");} catch (UnsupportedEncodingException e) {e.printStackTrace();}}return sb.toString();}/*** get方式的http請求** @param httpUrl 請求地址* @param paramStr 請求參數* @return 返回結果*/public static String doGet(String httpUrl,String paramStr) {HttpURLConnection connection = null;InputStream inputStream = null;BufferedReader bufferedReader = null;String result = null;// 返回結果字符串try {httpUrl += "?"+paramStr;// 創建遠程url連接對象URL url = new URL(httpUrl);// 通過遠程url連接對象打開一個連接,強轉成httpURLConnection類connection = (HttpURLConnection) url.openConnection();// 設置連接方式:getconnection.setRequestMethod("GET");// 設置連接主機服務器的超時時間:15000毫秒connection.setConnectTimeout(15000);// 設置讀取遠程返回的數據時間:60000毫秒connection.setReadTimeout(60000);// 設置請求頭connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 發送請求connection.connect();// 通過connection連接,獲取輸入流if (connection.getResponseCode() == 200) {inputStream = connection.getInputStream();// 封裝輸入流,并指定字符集bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));// 存放數據StringBuilder sbf = new StringBuilder();String temp;while ((temp = bufferedReader.readLine()) != null) {sbf.append(temp);sbf.append(System.getProperty("line.separator"));}result = sbf.toString();}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 關閉資源if (null != bufferedReader) {try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}if (null != inputStream) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}if (connection != null) {connection.disconnect();// 關閉遠程連接}}return result;}/*** post方式的http請求** @param httpUrl 請求地址* @param paramStr 請求參數* @return 返回結果*/public static String doPost(String httpUrl, String paramStr) {HttpURLConnection connection = null;InputStream inputStream = null;OutputStream outputStream = null;BufferedReader bufferedReader = null;String result = null;try {URL url = new URL(httpUrl);// 通過遠程url連接對象打開連接connection = (HttpURLConnection) url.openConnection();// 設置連接請求方式connection.setRequestMethod("POST");// 設置連接主機服務器超時時間:15000毫秒connection.setConnectTimeout(15000);// 設置讀取主機服務器返回數據超時時間:60000毫秒connection.setReadTimeout(60000);// 默認值為:false,當向遠程服務器傳送數據/寫數據時,需要設置為trueconnection.setDoOutput(true);// 設置傳入參數的格式:請求參數應該是 name1=value1&name2=value2 的形式。connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 通過連接對象獲取一個輸出流outputStream = connection.getOutputStream();// 通過輸出流對象將參數寫出去/傳輸出去,它是通過字節數組寫出的outputStream.write(paramStr.getBytes());// 通過連接對象獲取一個輸入流,向遠程讀取if (connection.getResponseCode() == 200) {inputStream = connection.getInputStream();// 對輸入流對象進行包裝:charset根據工作項目組的要求來設置bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));StringBuilder sbf = new StringBuilder();String temp;// 循環遍歷一行一行讀取數據while ((temp = bufferedReader.readLine()) != null) {sbf.append(temp);sbf.append(System.getProperty("line.separator"));}result = sbf.toString();}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 關閉資源if (null != bufferedReader) {try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}if (null != outputStream) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}if (null != inputStream) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}if (connection != null) {connection.disconnect();}}return result;}
}
🖊?最后總結
🖲要熟練掌握技巧,一定多多堅持練習:騏驥一躍,不能十步;駑馬十駕,功在不舍。