https://blog.csdn.net/ly20116/article/details/51086662
原文链接
当需要运行线程获取值时
public class weixin {
public static void main(String[] args) throws Exception {
new TokenThread().run();
Thread.sleep(1000*10);
try {
String access_token= TokenThread.accessToken.getAccessToken();
if (access_token==null){
System.out.println("票据没有") ;
}else {
System.out.println("票据" + access_token);
String path = "D:/Taa/tf1.mp4";
String mediaID = WeixinUtil.upload(path, access_token, "video");
System.out.println("mediaID是:" + mediaID);
} } catch (Exception e) {
e.printStackTrace(); } }}
http://www.cnblogs.com/fengzheng/p/5027630.html
但是都是用一个线程,sleep方法,太吃内存了
3.Java自带的定时器TimerTask
public class TokenTimer extends TimerTask {
public static String APPID =“”;
public static String APPSECRET= “”;
public static AccessToken accessToken=null;
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; @Override public void run() { accessToken=this.getAccessToken(); if (accessToken!=null){ Calendar calendar=Calendar.getInstance(); SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Calendar"+sf.format(calendar.getTime()));//打印当前的执行时间 System.out.println("票据"+accessToken.getAccessToken()); } return; } //获取AccessToken public AccessToken getAccessToken() { String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET); JSONObject jsonObject = WeixinUtil.httpsRequest(url,"GET",null); AccessToken accessToken = new AccessToken(); System.out.println(url); if (jsonObject != null) { accessToken.setAccessToken(jsonObject.getString("access_token")); accessToken.setExpiresIn(jsonObject.getInt("expires_in")); } return accessToken; }}
public class MyTimerText { public static void main(String[] args) { Timer timer=new Timer(); TokenTimer tokenTimer=new TokenTimer(); timer.schedule(tokenTimer,1000); } }
这个也一般,而且容易崩溃。
如果小伙伴有更好的方法,欢迎一起交流哦。