发表于: 2018-01-12 20:41:12
1 576
今天完成的事情:
学习微信API。设计接口文档。
//通过昨天获取的code 来获取用户的openid 然后通过openid 来获取对应的用户信息。这里只是简单的获取,然后代码还有很多改进的地方.没有做到最好。
public static net.sf.json.JSONObject getOpenInfo(String code, String appid, String appsecret)throws Exception{
StringBuffer buffer = new StringBuffer();
StringBuffer buffer1 = new StringBuffer();
String url ="https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid="+appid+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code";
URL urlConnection = new URL(url);
HttpURLConnection httpUrlConn =(HttpURLConnection)urlConnection.openConnection();
httpUrlConn.setRequestMethod("GET");
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();
httpUrlConn.disconnect();
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(buffer.toString());
//拉取用户信息
String access_token =(String)jsonObject.get("access_token");
String openid =(String)jsonObject.get("openid");
String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
URL urlConnection1 = new URL(userInfoUrl);
HttpURLConnection httpUrlConn1 =(HttpURLConnection)urlConnection1.openConnection();
httpUrlConn1.setRequestMethod("GET");
InputStream inputStream1 = httpUrlConn1.getInputStream();
InputStreamReader inputStreamReader1 = new InputStreamReader(inputStream1, "utf-8");
BufferedReader bufferedReader1 = new BufferedReader(inputStreamReader1);
String str1 = null;
while ((str1 = bufferedReader1.readLine()) != null) {
buffer1.append(str1);
}
bufferedReader1.close();
inputStreamReader1.close();
// 释放资源
inputStream1.close();
httpUrlConn1.disconnect();
net.sf.json.JSONObject jsonObject1 = net.sf.json.JSONObject.fromObject(buffer1.toString());
return jsonObject1;}
然后今天还设计了部分接口文档:
这里就截部分截图算了。具体的都在http://rap2.taobao.org 这个东西还挺好用。
明天计划的事情:
把接口文档搞定。
遇到的问题:
设计接口文档 思路不清晰。
收获:
微信API还挺好用。
评论