先在微信公众平台设置好模板和测试号:
地址:微信公众平台
下面是我的模板:
{
{riqi.DATA}} {
{beizhu.DATA}}
地区:{
{districtName.DATA}}
天气:{
{tianqi.DATA}}
风级:{
{windclass.DATA}}
风向:{
{winddir.DATA}}
最低气温: {
{low.DATA}}度
最高气温: {
{high.DATA}}度
今天是我们恋爱的第{
{lianai.DATA}}天
距离{
{you.DATA}}生日还有{
{shengri1.DATA}}天
距离{
{me.DATA}}生日还有{
{shengri2.DATA}}天
{
{lubarmonth.DATA}} {
{lunarday.DATA}}
宜:{
{fitness.DATA}}
不宜:{
{taboo.DATA}}
星座指数:{
{zhishu.DATA}}
{
{content.DATA}}
{
{caihongpi.DATA}}
{
{qingshi.DATA}}
{
{en.DATA}}
{
{zh.DATA}}
然后我们来到项目代码部分,项目使用springboot框架,使用前先导入依赖:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-mp -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
</dependency>
1、天行数据接口,第三方接口用于接入彩虹屁、黄历、情诗等,
public class CaiHongPiUtils {
public static String getCaiHongPi(String key) {
String httpUrl = "http://api.tianapi.com/caihongpi/index?key="+key;
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
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();
}
JSONObject jsonObject = JSONObject.parseObject(result);
JSONArray newslist = jsonObject.getJSONArray("newslist");
String content = newslist.getJSONObject(0).getString("content");
return content;
}
public static Map<String,String> getEveryday(String key) {
String httpUrl = "http://api.tianapi.com/everyday/index?key="+key;
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8