JAVA获取小程序二维码A,B,C接口,然后解析遇到的坑

本文讲述了在Java中如何获取并解析微信小程序的二维码,包括遇到的问题和解决方法。在尝试使用A、B、C三种接口时,发现文档描述不清晰且二维码无法正常解析。最终,通过截取微信返回的流并自定义处理,成功解析出二维码中的URL。
摘要由CSDN通过智能技术生成

一、问题描述


最近公司有个需求是,从小程序哪里获取二维码,然后从二维码中解析出URL来,自己定制下二维码。。。嫌微信生成的码太难看了。。。微信小程序的获取二维码的文档如下:https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html 看着这个文档。。。就想吐槽下。。写的太含糊了。



竟然没写返回值是什么。。最后得知,它是以流的形式返回。。。文档里有三A、B、C三种类型的接口,不清楚微信怎么想的。。。分这么多种接口,做什么。。A,B接口有生成次数限制,B接口没有限制。。


测试代码如下:

public class Test {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=你的access_token");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();
            paramJson.put("scene", "a=1234567890");
            paramJson.put("page", "pages/index/index");
            paramJson.put("width", 430);
            paramJson.put("auto_color", true);
            /**
             * line_color生效
             * paramJson.put("auto_color", false);
             * JSONObject lineColor = new JSONObject();
             * lineColor.put("r", 0);
             * lineColor.put("g", 0);
             * lineColor.put("b", 0);
             * paramJson.put("line_color", lineColor);
             * */

            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            OutputStream os = new FileOutputStream(new File("/Users/Xxxx/Music/abc.png"));
            int len;
            byte[] arr = new byte[1024];
            while ((len = bi
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值