微信公众号开发--双色球彩票开奖结果(Java版)

可扫描下方二维码关注 你我杂志刊 或微信搜索公众号 你我杂志刊
微信公众号开发--双色球彩票开奖结果(Java版)_第1张图片
然而请求这个接口似乎有点慢了。在PC端的浏览器请求也并不是很快!
这次主要使用了免费的 开彩API http://www.opencai.net/apifree/
返回json数据格式
微信公众号开发--双色球彩票开奖结果(Java版)_第2张图片
以下就是对 开彩API 进行请求到数据并解析成Map对象。

package com.zhenqi.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import flexjson.JSONDeserializer;

/**
 * 开采API  http://www.opencai.net/apifree/
 * @author wuming
 *
 */
public class OpenCaiUtil {

    private static final String SHUANGSEQIU_URL="http://f.apiplus.net/ssq.json";

    public static String shuangSeQiu(String keyWord) throws Exception{
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { myX509TrustManager }, null);

        StringBuffer buf=new StringBuffer();
        if("双色球".equals(keyWord)){
        URL url=new URL(SHUANGSEQIU_URL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        if (conn instanceof HttpsURLConnection) {
            ((HttpsURLConnection) conn).setSSLSocketFactory(sslcontext.getSocketFactory());
        }

        conn.setConnectTimeout(10000); // 设置相应超时
        conn.setRequestMethod("GET");
        int statusCode = conn.getResponseCode();
        if (statusCode != HttpURLConnection.HTTP_OK) {
            System.out.println("Http错误码:" + statusCode);
        }
        //URLConnection open = url.openConnection();
        InputStream input = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
        StringBuffer stringBuffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        }
        reader.close();
        input.close();
        conn.disconnect();
        JSONDeserializer> json = new JSONDeserializer>();
        Map map = json.deserialize(stringBuffer.toString());
        List> list_result = (List>) map.get("data");

        for(int j=0;j res_map=list_result.get(j);
            String expect=res_map.get("expect").toString();
            String opencode=res_map.get("opencode").toString();
            String opentime=res_map.get("opentime").toString();

            buf.append("第");
            buf.append(expect);
            buf.append("期开奖结果\n");
            buf.append("开奖日期:");
            buf.append(opentime);
            buf.append("\n");
            buf.append(opencode);
            buf.append("\n\n");
        }
        }else{
            buf.append("该【");
            buf.append(keyWord);
            buf.append("】彩票种类正在完善中……,敬请期待!");
        }
        return buf.toString();

    }

    private static TrustManager myX509TrustManager = new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    };
   }

而后在servlet将其输出即可,可参考 微信公众号开发–关注后自动回复(Java记录篇)
有讲到该如何在servlet 中输出。
最终实现效果:
微信公众号开发--双色球彩票开奖结果(Java版)_第3张图片
可扫描下方二维码关注 你我杂志刊 或微信搜索公众号 你我杂志刊
微信公众号开发--双色球彩票开奖结果(Java版)_第4张图片

你可能感兴趣的:(微信公众号)