发表于: 2018-01-01 22:23:10
1 471
今天完成的事情
写了与微信相关的工具类
获取code:
/**
* 获取code
*/
public String getCode(HttpServletRequest request){
System.out.println(request.getQueryString());
String cc = request.getQueryString();
String cc2 = cc.replace("code=","");
String code = cc2.replace("&state=STATE","");
return code;
}
获取acces_token和openId:
/**
* 获取access_token---[]
* 获取OpenId---[]
*/
public String[] getAccess_toekn(String code){
//替换参数
String url = access_token_api.replace("APPID",appId).replace("SECRET",appSecret).replace("CODE",code);
//调用接口,获取access_token,openId
JSONObject jsonObject = WeixinUtil.httpRequest(url, "GET", null);
String access_token = jsonObject.getString("access_token");
String refresh_token = jsonObject.getString("refresh_token");
String openid = jsonObject.getString("openid");
String scope = jsonObject.getString("scope");
String[] tokenAndOpenId = new String[]{access_token,openid};
System.out.println(jsonObject.toString());
return tokenAndOpenId;
}
拉取用户数据:
/**
* 获取data
* 用户数据
* [0]:sex,[1]:city,[2]:province
*/
public String[] getUserData(String access_token,String openid){
String O_url = O_url_1.replace("ACCESS_TOKEN",access_token).replace("OPENID",openid);
//调用接口,获取用户信息
JSONObject data = WeixinUtil.httpRequest(O_url, "GET", null);
String sex = data.getString("sex");
String city = data.getString("city");
String province = data.getString("province");
String[] userData = new String[]{sex,city,province};
System.out.println(data.toString());
return userData;
}
还需要编写一个发送https请求的方法:
/**
* 描述: 发起https请求并获取结果
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr)
太长不贴了
明天的计划
完善手机,邮箱验证模块,部署到服务器
遇到的问题
无
收获
工具类的封装
评论