首页 > 技术文章 > 转:微信生成二维码java

wangtj-19 2016-09-23 16:55 原文

  1. package com.wepayweb.weixin.util.device;  
  2. /***  
  3.  * V型知识库 www.vxzsk.com  
  4.  */  
  5. import java.io.BufferedReader;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10. import java.net.URLConnection;  
  11. public class DeviceTest {  
  12.        
  13.     public static String sendGet(String url, String charset, int timeout)  
  14.       {  
  15.         String result = "";  
  16.         try  
  17.         {  
  18.           URL u = new URL(url);  
  19.           try  
  20.           {  
  21.             URLConnection conn = u.openConnection();  
  22.             conn.connect();  
  23.             conn.setConnectTimeout(timeout);  
  24.             BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));  
  25.             String line="";  
  26.             while ((line = in.readLine()) != null)  
  27.             {  
  28.                
  29.               result = result + line;  
  30.             }  
  31.             in.close();  
  32.           } catch (IOException e) {  
  33.             return result;  
  34.           }  
  35.         }  
  36.         catch (MalformedURLException e)  
  37.         {  
  38.           return result;  
  39.         }  
  40.            
  41.         return result;  
  42.       }  
  43.    
  44.     /**  
  45.      * @param args  
  46.      */  
  47.     public static void main(String[] args) {  
  48.         // TODO Auto-generated method stub  
  49.         String access_token="";//  
  50.         String product_id="";  
  51.         String data = DeviceTest.sendGet("https://api.weixin.qq.com/device/getqrcode?access_token="+access_token+"&product_id="+product_id, "utf-8", 30000);  
  52.         System.out.println("获取设备的deviceid和二维码:"+data);  
  53.     }  
  54.    
  55. }  


执行main方法即可,access_token  和product_id 自行获取,产品的id号 可登陆公众号管理平台 硬件列表查询。

 

获取的二维码 是个地址字符串 需要读者用二维码生成器生成图片

推荐阅读