JAVA——微信小程序获取用户的openId

首先说一下,
这里是获取微信openId方法中的核心代码,并不是整个class类,只要你稍有耐心,跟着我的思路一起看一下,立马就会!

			//第一步:前端需要获取到用户同意授权的code给你传过来【code不用你管,拿String接收传来的参数即可】
			//url中的参数说明:
			//appId:不用解释,你懂得
			//appSecret: 不用解释,你懂得
			//code:前端传来的code
            String strURL = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+appSecret+"&js_code="+code+"&grant_type=authorization_code";
			//第二步:发起请求【copy就行了】
            try {
                URL url = new URL(strURL);// 创建连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestMethod("POST"); // 设置请求方式
                connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
                connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
                //开始发送请求
                connection.connect();
                OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
                out.flush();
                out.close();
                int ResponseCode = connection.getResponseCode();
                InputStream is = null;
                if (ResponseCode == 200) {
                    is = connection.getInputStream();
                } else {
                    is = connection.getErrorStream();
                }
                // 第三步:读取响应
                ///在这里说一下,获取微信返回的方式有两种,随便挑一种即可
                //第一种:获取返回信息方式【比较骚气,用byte解析后获取】
                int length = (int) connection.getContentLength();// 获取长度
                if (length != -1) {
                    byte[] data = new byte[length];
                    byte[] temp = new byte[512];
                    int readLen = 0;
                    int destPos = 0;
                    while ((readLen = is.read(temp)) > 0) {
                        System.arraycopy(temp, 0, data, destPos, readLen);
                        destPos += readLen;
                    }
                    String result = new String(data, "UTF-8"); // utf-8编码
                    Map<String, Object> resultMap = com.alibaba.fastjson.JSONObject.parseObject(result);
                    //【这里是你获取到openId的逻辑判断】
                    //【plifeUser是我当时的用户实体类,保存openId,复制到你的项目可以删掉】
                    plifeUser.setOpenId((String) resultMap.get("openid"));
                }
                //第二种:获取返回信息方式【更简单一点吧,但是第一种更骚气点】
                /*String contentEncoding = connection.getContentEncoding();
                String body= IOUtils.toString(is, contentEncoding);
                System.out.println(body);
                org.json.JSONObject jsonObject=new org.json.JSONObject(body);
                String openId=jsonObject.getString("openid");
                //【这里是你获取到openId的逻辑判断】
                //【plifeUser是我当时的用户实体类,保存openId,复制到你的项目可以删掉】
                plifeUser.setOpenId(openId);*/
            } catch (IOException e) {
				//  LOG.error("Exception occur when send http post request!", e);
            }

好了,到此为止,微信小程序获取用户的OpenId就完成了,基板上你copy就行了。记得导包哦!!!!!!!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值