微信小程序> Java获取小程序带参二维码太阳码-微信小程序太阳码怎么生成-小程序码在线生成

Java获取小程序带参二维码太阳码-微信小程序太阳码怎么生成-小程序码在线生成

浏览量:3859 时间: 来源:回憶是如此忧伤
获取小程序码1.

官方API地址:https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html

2.

首先使用官方提供的接口B:适用于需要的码数量极多的业务场景

https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_tokenACCESS_TOKEN3.

POST参数说明(官方)

4.

注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中scene字段的值,再做处理逻辑。使用如下代码可以获取到二维码中的scene字段的值。调试阶段可以使用开发工具的条件编译自定义参数scenexxxx进行模拟,开发工具模拟时的scene的参数值需要进行urlencode

5.

以上都是官方文档所示,如使用A接口或C接口可参考官方API,一下开始切入正题java获取access_token以及接口调用

6.

获取access_token:

publicstaticvoidGetUrlS()throwsClientProtocolException,IOException{HttpGethttpGetnewHttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_typeclient_credentialappid"+appid+"secret"+secret);HttpClienthttpClientHttpClients.createDefault();HttpResponsereshttpClient.execute(httpGet);HttpEntityentityres.getEntity();StringresultEntityUtils.toString(entity,"UTF-8");JSONObjectjsonsJSONObject.fromObject(result);Stringexpires_injsons.getString("expires_in");//缓存if(Integer.parseInt(expires_in)7200){//okStringaccess_tokenjsons.getString("access_token");System.out.println("access_token:"+access_token);contaccess_token;}else{System.out.println("出错获取token失败!");}}7.

开始调研接口B生成二维码:

publicstaticvoidmain(String[]args){try{GetUrlS();URLurlnewURL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token"+access_token);HttpURLConnectionhttpURLConnection(HttpURLConnection)url.openConnection();httpURLConnection.setRequestMethod("POST");//提交模式//conn.setConnectTimeout(10000);//连接超时单位毫秒//conn.setReadTimeout(2000);//读取超时单位毫秒//发送POST请求必须设置如下两行httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);//获取URLConnection对象对应的输出流PrintWriterprintWriternewPrintWriter(httpURLConnection.getOutputStream());//发送请求参数JSONObjectparamJsonnewJSONObject();paramJson.put("scene","a1234567890");paramJson.put("page","pages/index/index");paramJson.put("width",430);paramJson.put("auto_color",true);/***line_color生效*paramJson.put("auto_color",false);*JSONObjectlineColornewJSONObject();*lineColor.put("r",0);*lineColor.put("g",0);*lineColor.put("b",0);*paramJson.put("line_color",lineColor);**/printWriter.write(paramJson.toString());//flush输出流的缓冲printWriter.flush();//开始获取数据BufferedInputStreambisnewBufferedInputStream(httpURLConnection.getInputStream());OutputStreamosnewFileOutputStream(newFile("C:/Users/Waitforyou/Desktop/abc.png"));intlen;byte[]arrnewbyte[1024];while((lenbis.read(arr))!-1){os.write(arr,0,len);os.flush();}os.close();}catch(Exceptione){e.printStackTrace();}}

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎