Java获取小程序带参二维码太阳码-微信小程序太阳码怎么生成-小程序码在线生成
获取小程序码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();}}声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。

