欢迎光临,了解微信小程序开发,就上易用通!

java调用微信小程序API createwxaqrcode 产生二维码

发布:2018-01-25 10:04浏览: 来源:网络 作者:tianshu

调用产生二维码的接口一直乱码,一直没想出为什么乱码,囧,郁闷之极,还好九爷慷慨分享了文章,得知返回的是二进制,createwxaqrcode接口并不复杂,只是在官方接口并没声明返回内容,只要知道返回的内容就好办了,代码如下


post请求
  1. public static String httpPostWithJSON(String url, String json)
  2.                         throws Exception {
  3.                 String result = null;
  4.                 // 将JSON进行UTF-8编码,以便传输中文
  5.                 String encoderJson = URLEncoder.encode(json, HTTP.UTF_8);
  6.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  7.                 HttpPost httpPost = new HttpPost(url);
  8.                 httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
  9.                 StringEntity se = new StringEntity(json);
  10.                 se.setContentType(CONTENT_TYPE_TEXT_JSON);
  11.                 se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
  12.                                 APPLICATION_JSON));
  13.                 httpPost.setEntity(se);
  14.                 // httpClient.execute(httpPost);
  15.                 HttpResponse response = httpClient.execute(httpPost);
  16.                 if (response != null) {
  17.                         HttpEntity resEntity = response.getEntity();
  18.                         if (resEntity != null) {
  19.                                 InputStream instreams = resEntity.getContent(); 
  20.                                 saveToImgByInputStream(instreams, "D:\\", "apr.jpg");
  21.                         }
  22.                 }
  23.                 return result;
  24.         }

将二进制转换成文件保存
  1. /**
  2.      * 将二进制转换成文件保存
  3.      * @param instreams 二进制流
  4.      * @param imgPath 图片的保存路径
  5.      * @param imgName 图片的名称
  6.      * @return 
  7.      *      1:保存正常
  8.      *      0:保存失败
  9.      */
  10.     public static int saveToImgByInputStream(InputStream instreams,String imgPath,String imgName){
  11.         int stateInt = 1;
  12.         if(instreams != null){
  13.             try {
  14.                 File file=new File(imgPath,imgName);//可以是任何图片格式.jpg,.png等
  15.                 FileOutputStream fos=new FileOutputStream(file);
  16.                 byte[] b = new byte[1024];
  17.                 int nRead = 0;
  18.                 while ((nRead = instreams.read(b)) != -1) {
  19.                     fos.write(b, 0, nRead);
  20.                 }
  21.                 fos.flush();
  22.                 fos.close();                
  23.             } catch (Exception e) {
  24.                 stateInt = 0;
  25.                 e.printStackTrace();
  26.             } finally {
  27.             }
  28.         }
  29.         return stateInt;
  30.     }





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。