Java--对接微信第六篇之根据请求url及所需参数返回json

版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接 ,博主地址:https://blog.csdn.net/qq_31122833。 https://blog.csdn.net/qq_31122833/article/details/83824876

与微信对接就是与别人的接口对接,调用别人接口获取微信数据的过程。

首先我们需要指定微信那边需要什么参数,接口返回哪些json数据。知道了这些,就只有调用接口的事情了。

所以,我们需要一个方法,能够传递url+access_token+params然后将返回结果组装成我们需要格式的方法。

public static String getJsonData(JSONObject jsonParam,String urls,String access_token) {
        StringBuffer sb=new StringBuffer();
        urls += access_token;
        try {
            // 创建url资源
            URL url = new URL(urls);
            // 建立http连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出
            conn.setDoOutput(true);
            // 设置允许输入
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            // 转换为字节数组
            byte[] data = (jsonParam.toString()).getBytes();
            // 设置文件长度
            conn.setRequestProperty("Content-Length", String.valueOf(data.length));
            // 设置文件类型:
            conn.setRequestProperty("contentType", "application/json");
            // 开始连接请求
            conn.connect();
            OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
            // 写入请求的字符串
            out.write((jsonParam.toString()).getBytes());
            out.flush();
            out.close();
            // 请求返回的状态
            if (HttpURLConnection.HTTP_OK == conn.getResponseCode()){
                // 请求返回的数据
                InputStream in1 = conn.getInputStream();
                try {
                    String readLine=new String();
                    BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
                    while((readLine=responseReader.readLine())!=null){
                        sb.append(readLine).append("\n");
                    }
                    responseReader.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

然后写一个main方法调用测试:

public static void main(String[] args) throws ClientProtocolException, IOException {
        Map<String,Object> map = new HashMap<>();
        map.put("begin_date","2018-11-01");
        map.put("end_date","2018-11-01");
        String outStr = JsonUtil.mapToJson(map);
        String s = WeiXinUtil.getJsonData(JSONObject.fromObject(outStr),
                "https://api.weixin.qq.com/datacube/getarticlesummary?access_token=",
                "access_token");
        Map maps=(Map) JSON.parse(s);
        System.out.println(s);
        List<Map<String, Object>> mapList = JsonUtil.parseJSONList(maps.get("list").toString());
    }

 好了,你可以用这个方法随便改造一下,就可以应对很多微信接口开发了

猜你喜欢

转载自blog.csdn.net/qq_31122833/article/details/83824876