分爲兩步、要生成小程序的二維碼,都是調用小程序本身的apihtml
一、先獲取tokenjson
二、而後再生成二維碼小程序
HttpGet httpGet = new HttpGet( "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret ); HttpClient httpClient = HttpClients.createDefault(); HttpResponse res = httpClient.execute(httpGet); HttpEntity entity = res.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); JSONObject jsons = JSONObject.fromObject(result); String expires_in = jsons.getString("expires_in"); //緩存 if(Integer.parseInt(expires_in)==7200){ //ok String access_token = jsons.getString("access_token"); System.out.println("access_token:"+access_token);
}
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式 // 發送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", page); paramJson.put("width", 430); paramJson.put("auto_color", true); printWriter.write(paramJson.toString()); // flush輸出流的緩衝 printWriter.flush(); //開始獲取數據 bis = new BufferedInputStream(httpURLConnection.getInputStream()); bos = new ByteArrayOutputStream(); bos.write(bis); byte[] bs = bos.toByteArray(); printWriter.close(); bos.close(); bis.close();