公告:1.恭喜本站微信硬件蓝牙教程阅读总数突破100万次,微信jsapi阅读总数突破10万次... 2.友情交换/给本站留言

案例归档:  微信硬件获取设备deviceid和二维码—实现代码

package com.wepayweb.weixin.util.device;
/***
 * V型知识库 www.vxzsk.com
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DeviceTest {
     
    public static String sendGet(String url, String charset, int timeout)
      {
        String result = "";
        try
        {
          URL u = new URL(url);
          try
          {
            URLConnection conn = u.openConnection();
            conn.connect();
            conn.setConnectTimeout(timeout);
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
            String line="";
            while ((line = in.readLine()) != null)
            {
             
              result = result + line;
            }
            in.close();
          } catch (IOException e) {
            return result;
          }
        }
        catch (MalformedURLException e)
        {
          return result;
        }
         
        return result;
      }
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String access_token="";//
        String product_id="";
        String data = DeviceTest.sendGet("https://api.weixin.qq.com/device/getqrcode?access_token="+access_token+"&product_id="+product_id, "utf-8", 30000);
        System.out.println("获取设备的deviceid和二维码:"+data);
    }
 
}