My___Android 2014-12-17 06:00 采纳率: 0%
浏览 6057

大家感觉这种获取微信Access_Token方法如何

public class WxTokenUtil {

private static String FileName = "WxTokenUtil.properties";
private static String APPID = "fsdafsd";
private static String APPSECRET = "dfsdsaf";

public synchronized static String getAccessToken() {

    try{
    // 文件获取token值及时间
    Properties prop = new Properties();// 属性集合对象
    InputStream fis  =WxTokenUtil.class.getClassLoader().getResourceAsStream(FileName);  
    prop.load(fis);// 将属性文件流装载到Properties对象中
    fis.close();// 关闭流

    String access_token = prop.getProperty("access_token");
    String expires_in = prop.getProperty("expires_in");
    String last_time = prop.getProperty("last_time");

    int int_expires_in = 0;
    long long_last_time = 0;

    try{
        int_expires_in = Integer.parseInt(expires_in);
        long_last_time = Long.parseLong(last_time);

    }catch(Exception e){

    }


    long current_time = System.currentTimeMillis();

    // 如果token时间超时,重新获取
        if ((current_time - long_last_time) / 1000 >= int_expires_in) {
            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                    + APPID + "&secret=" + APPSECRET;

           JSONObject jobject = httpRequest(url);

           String  j_access_token = (String) jobject.get("access_token");
           String  j_expires_in = (String) jobject.get("expires_in");




           //保存 
            if(j_access_token!=null && j_expires_in!=null){
                prop.setProperty("access_token", j_access_token);     
                prop.setProperty("expires_in", j_expires_in);     
                prop.setProperty("last_time", System.currentTimeMillis()+"");    

                URL url_ = WxTokenUtil.class.getClassLoader().getResource(FileName);
                FileOutputStream fos=  new FileOutputStream(new File(url_.toURI()));
                prop.store(fos, null);
                fos.close();// 关闭流 
            }
            return j_access_token;
        } else {
            return access_token;
        }
    }
    catch(Exception e){
        return null;
    }


}

// --
public synchronized static JSONObject httpRequest(String requestUrl) {
    JSONObject jsonObject = null;
    StringBuffer buffer = new StringBuffer();
    try {

        URL url = new URL(requestUrl);
        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                .openConnection();

        httpUrlConn.setDoOutput(true);
        httpUrlConn.setDoInput(true);
        httpUrlConn.setUseCaches(false);
        // 设置请求方式(GET/POST)
        httpUrlConn.setRequestMethod("GET");

        httpUrlConn.connect();

        // 将返回的输入流转换成字符串
        InputStream inputStream = httpUrlConn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(
                inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(
                inputStreamReader);

        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        // 释放资源
        inputStream.close();
        inputStream = null;
        httpUrlConn.disconnect();
        jsonObject = JSONObject.fromObject(buffer.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return jsonObject;
}
  • 写回答

3条回答 默认 最新

  • q107770540 2014-12-17 06:52
    关注

    能获取到数据么? 之前做微信公共号开发时,用的是第三方的dll

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?