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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条