何谓历史上的今天
回顾历史的长河,历史是生活的一面镜子;以史为鉴,可以知兴衰;历史上的每一天,都是喜忧参半;可以了解历史的这一天发生的事件,借古可以鉴今,历史是不能忘记的。查看历史上每天发生的重大事情,增长知识,开拓眼界,提高人文素养。
寻找接口(数据源)
要实现查询“历史上的今天”,首先我们要找到相关数据源。笔者经过搜索发现,今年来很多公司都开放免费了“历史上的今天”公共接口调用。只需要创建一个账号,然后根据key来http请求接口,即可返回一个json或xml的数据,非常的方面,最重要的还是免费,由于很多就不一一举例了
代码实现
package net.ibloger.wechat.core.api;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import net.ibloger.wechat.core.WeChatConfig;
import net.ibloger.wechat.utils.HttpProxyUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
/**
* 历史上的今天查询
*
* @author X-rapido
* @description 回顾历史的长河,历史是生活的一面镜子; 以史为鉴,可以知兴衰,回复:历史上的今天,看看都发生了什么重大事件
*/
public class HistoryTodayAPI {
private static Logger logger = Logger.getLogger(HistoryTodayAPI.class);
/**
* 历史上的今天,调用ShowApi接口
* @param dateTime dateTime为空代表今天
* @return
*/
public String getContent(String dateTime) {
Calendar calendar = Calendar.getInstance();
String month = calendar.get(Calendar.MONTH) + 1 + ""; // 0代表1月
month = Integer.valueOf(month) < 10 ? "0" + month : month; // 小于10个位补0
int day = calendar.get(Calendar.DATE);
if (dateTime.equals("昨天")){
day--;
dateTime = month + day;
} else if(dateTime.equals("明天")){
day++;
dateTime = month + day;
} else if(dateTime.equals("前天")){
day-=2;
dateTime = month + day;
} else if(dateTime.equals("后天")){
day+=2;
dateTime = month + day;
} else if(dateTime.equals("今天")){
dateTime = month + day;
} else {
// 判断数字日期的正确性
if (StringUtils.isNotBlank(dateTime)) {
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
try {
sdf.setLenient(false);
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期
System.out.println(sdf.parse(dateTime));
} catch (ParseException e) {
return null;
}
}else {
dateTime = month + day;
}
}
String reqUrl = WeChatConfig.show_api_historyToday_url;
try {
reqUrl = reqUrl.replace("{appid}", WeChatConfig.show_api_appid).replace("{timestamp}", new Date().getTime()+"").replace("{date}", dateTime).replace("{sign}", WeChatConfig.show_api_secret);
logger.info("请求历史上的今天链接:"+reqUrl);
// 查询并获取返回结果
String json = HttpProxyUtil.sendGet(reqUrl);
StringBuffer sb = new StringBuffer();
sb.append(Character.toChars(0xe027)); // 图标
sb.append(" 历史上的 ").append(month).append("月").append(day).append("日\n\n");
if (!"".equals(json) && json.trim().length() > 0) {
JSONObject jsonObject = JSONObject.fromObject(json);
if (jsonObject.getJSONObject("showapi_res_body").get("ret_code").toString().equals("0")) { // 0表示成功
JSONArray array = jsonObject.getJSONObject("showapi_res_body").getJSONArray("list");
for (int i = 0; i < (array.size() > 25 ? 25 :array.size()); i++) {
JSONObject jo = (JSONObject) array.get(i); // 返回day、img、month、title、year参数
sb.append(jo.get("year")).append("年 ").append(jo.get("title")).append("\n\n");
}
}
}
return sb.toString();
} catch (Exception e) {
logger.error("历史上的今天出现异常", e);
e.printStackTrace();
}
return null;
}
/**
* 历史上的今天使用指南
* @return
*/
public String getHistoryTodayUsage() {
StringBuffer buffer = new StringBuffer();
buffer.append(Character.toChars(0xe04a)).append("历史上的今天使用指南:").append("\n\n");
buffer.append(" 回复:历史上的+前天/昨天/明天/后天").append("\n\n");
buffer.append(" 历史上的+数字月份日期").append("\n\n");
buffer.append("示例:").append("\n");
buffer.append(" 历史上的明天").append("\n");
buffer.append(" 历史上的前天").append("\n\n");
buffer.append(" 历史上的0922").append("\n\n");
buffer.append("回复“?”显示主菜单");
return buffer.toString();
}
/**
* 通过main在本地测试
* @param args
*/
public static void main(String[] args) {
HistoryTodayAPI htService = new HistoryTodayAPI();
System.out.println(htService.getContent("昨天"));
System.out.println("__________");
System.out.println(htService.getContent("后天"));
}
}以上代码中,用到了Http的请求,封装成了HttpProxyUtil.java,WeChatConfig.java用于存储微信配置的appid等信息
// 请求的url地址为
show_api_historyToday_url=http://route.showapi.com/119-42?showapi_appid={appid}&showapi_timestamp={timestamp}&date={date}&showapi_sign={sign}未经允许请勿转载:程序喵 » 微信公众号开发教程第13篇——应用实例之历史上的今天
程序喵