微信公众号开发生成token
微信开发必不可少的参数
private static void getToken(){ //替换成自己的appid 和 appsecret String url= TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET); //发送请求 String tokenStr = MyUtil.get(url); JSONObject obj = JSONObject.fromObject(tokenStr); String accessToken = obj.getString("access_token"); String expriesIn = obj.getString("expires_in"); //对结果进行保存 at = new AccessToken(accessToken, expriesIn); System.out.println(at); } //此处的处理目的,在存在一个有意义token时,不会重复请求微信服务器 public static String getAccessToken(){ if(at == null || at.isExpries()){ getToken(); } return at.getAccess_token() ; } |
AccessToken类
private String access_token ; private long dendline ; public String getAccess_token() { return access_token; } public void setAccess_token(String access_token) { this.access_token = access_token; } public long getDendline() { return dendline; } public void setDendline(long dendline) { this.dendline = dendline; } public AccessToken(String access_token , String expriesIn) { super(); this.access_token = access_token; this.dendline = System.currentTimeMillis() + Integer.parseInt(expriesIn)*1000 ; }
public boolean isExpries() { return System.currentTimeMillis() > dendline ; } |