1️⃣.已获取小程序的access_token 为例,通过Get请求url
1 1 import com.alibaba.fastjson.JSONObject; 2 2 3 3 String wechatUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; 4 4 5 5 String appId = "xxxx"; 6 6 String secret = "xxxxx"; 7 7 8 8 String getUrl = MessageFormat.format(wechatUrl, appId, secret); 9 9 1010 URL url = new URL(urlStr); 1111 JSONObject jsonObject; 1212 try (InputStream inputStream = url.openStream()) { 1313 jsonObject = JSONObject.parseObject(IOUtils.toString(inputStream)); 1414 } 1515 if (jsonObject.containsKey("errmsg")) { 1616 throw new LobsterException("获取token,原因:" + jsonObject.getString("errmsg")); 1717 } 1818 return jsonObject.getString("access_token");
2️⃣.已获取小程序码为例,通过Post请求url 也可以参考https://www.cnblogs.com/bchange/p/9156178.html
1.先设置小程序码参数
1 1 package com.ieou.lobster.dto; 2 2 3 3 public class MiniProgramsCode { 4 4 5 5 private String scene; 6 6 private String page = "pages/shop/shop"; 7 7 private int width = 300; 8 8 private Boolean auto_color; 9 9 private Object line_color; 1010 1111 public String getScene() { 1212 return scene; 1313 } 1414 1515 public void setScene(String scene) { 1616 this.scene = scene; 1717 } 1818 1919 public String getPage() { 2020 return page; 2121 } 2222 2323 public void setPage(String page) { 2424 this.page = page; 2525 } 2626 2727 public int getWidth() { 2828 return width; 2929 } 3030 3131 public void setWidth(int width) { 3232 this.width = width; 3333 } 3434 3535 public Boolean getAuto_color() { 3636 return auto_color; 3737 } 3838 3939 public void setAuto_color(Boolean auto_color) { 4040 this.auto_color = auto_color; 4141 } 4242 4343 public Object getLine_color() { 4444 return line_color; 4545 } 4646 4747 public void setLine_color(Object line_color) { 4848 this.line_color = line_color; 4949 } 5050 }
2.对URL进行Post请求
1 1 import org.apache.http.HttpResponse; 2 2 import org.apache.http.client.methods.HttpPost; 3 3 import org.apache.http.entity.StringEntity; 4 4 import org.apache.http.impl.client.CloseableHttpClient; 5 5 import org.apache.http.impl.client.HttpClientBuilder; 6 6 7 7 public String getMiniProgramsQRCode(Integer restaurantId, String tableNumber, Integer tableNumberId) throws Exception { 8 8 9 9 String accessToken = getMiniProgramsAccessToken(); 1010 1111 MiniProgramsCode miniProgramsCode = new MiniProgramsCode(); 1212 1313 String str = restaurantId.toString() + "*" + tableNumberId.toString() + "*" + tableNumber; 1414 miniProgramsCode.setScene(str); 1515 miniProgramsCode.setAuto_color(true); 1616 //设置颜色 1717 MiniProgramsCodeRgb miniProgramsCodeRgb = new MiniProgramsCodeRgb(); 1818 miniProgramsCode.setLine_color(miniProgramsCodeRgb); 1919 2020 CloseableHttpClient build = HttpClientBuilder.create().build(); 2121 String postUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken; 2222 HttpPost httpPost = new HttpPost(postUrl); 2323 httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); 2424 String jsonStr = JSONObject.toJSONString(miniProgramsCode); 2525 StringEntity se = new StringEntity(jsonStr,"UTF-8"); // 中文乱码解决 2626 se.setContentType("text/json"); 2727 se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 2828 httpPost.setEntity(se); 2929 HttpResponse httpResponse = build.execute(httpPost); 30 31 if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(httpResponse.getEntity());// 返回json格式: JSONObject response = JSON.parseObject(result); } 32 3330 InputStream inputStream = httpResponse.getEntity().getContent(); //返回流格式 3431 }