Timer timer = new Timer();
myTask = new MyTask();
timer.schedule(myTask, 0, 1000 * 60 * 60);
/***
* 一天有获取2000次的限制 ,设置1小时获取一次AccessToken防止超出请求限制
*
* @author wWX323797
*
*/
public class MyTask extends TimerTask {
public static WechatUtil wechatUtil = new WechatUtil();
public static JSONObject access_token_json;
public static String ACCESS_TOKEN = null;
@Override
public void run() {
access_token_json = wechatUtil.getAccess_token();
ACCESS_TOKEN = access_token_json.getString("access_token");
}
}
public JSONObject getAccess_token() {
try {
URL url = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appSecret + "");
jsonObject = getFromWechat(url);
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}