2 my   android My___Android 于 2014.12.17 14:00 提问

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

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
q107770540   Ds   Rxr 2014.12.17 14:52

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

qq_37473322
qq_37473322   2017.02.07 23:22

thanks,根据你的代码,已经成功在自己的项目上实现了access_token的长期有效性,
感觉你的方法比柳峰老师的好多了,他那个会报空指针错误,还占资源,
不过还是有个小小的疑惑附图图片说明
为啥每次获取access_token接口时都是加载build下的access_token.properties而不是config下access_token.properties,
而且当access_token失效时,从微信服务器获取新的access_token,然后更新的是build下的access_token.properties而不是
config下access_token.properties,也就是说config下access_token.properties只是第一次被加载过然后就没用了。
我很困惑,望前辈能解惑

shenxiaomo1688
shenxiaomo1688 怎么你配置了两个Properties 的?
10 个月之前 回复
shenxiaomo1688
shenxiaomo1688   2017.03.14 20:23

请问Properties这个文件要怎么配置?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!