赞
踩
package com.nssol.learning; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; import net.sf.json.JSONObject; /** * 调用API接口判断日期是否是工作日 周末还是节假日 * * @author i * */ public class HolidayUtil { /** * @param httpArg * :参数 * @return 返回结果 */ public static int request( String httpArg) { String httpUrl="http://api.goseek.cn/Tools/holiday"; BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); httpUrl = httpUrl + "?date=" + httpArg; int d=0; try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); 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(); JSONObject ob= JSONObject.fromObject(result); if(ob!=null){ d=Integer.parseInt(ob.getString("data")); } } catch (Exception e) { e.printStackTrace(); } return d; } public static void main(String[] args) { //判断今天是否是工作日 周末 还是节假日 SimpleDateFormat f=new SimpleDateFormat("yyyyMMdd"); String httpArg="20190902";//f.format(new Date()); System.out.println(httpArg); int n = request(httpArg); System.out.println(n); } }
需要注意的是,运行这个代码需要导入net.sf.json.JSONObject
这个JAR包
我们可以在网上搜索对应JAR包导入到项目中即可
下载地址为:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/
我们可以使用maven中的pom.xml
配置文件来导入JAR包
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。