微信工具类(常用接口)的整理

时间:2014-11-05 21:09:35   收藏:0   阅读:406
 1 /**
 2      * 根据微信用户的AppId和AppSecret获取access token
 3      * @param wxAppId 微信用户的AppId
 4      * @param wxAppSecret 微信用户的AppSecret
 5      * @return 微信服务器的access token
 6      */
 7     public static String getAccessToken(String wxAppId, String wxAppSecret) {
 8         String retStr = "";
 9         
10         try {
11             String baseUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
12                     + wxAppId + "&secret=" + wxAppSecret;
13             
14             HttpClient httpclient = new DefaultHttpClient();
15             HttpGet httpget = new HttpGet(baseUrl);
16             HttpResponse response = httpclient.execute(httpget);
17             String wxResultStr = EntityUtils.toString(response.getEntity());
18             // 返回的数据类似【{"access_token":"ACCESS_TOKEN","expires_in":7200}】
19             JSONObject objRoot = new JSONObject(wxResultStr);
20             retStr = objRoot.getString("access_token");
21         } catch (Exception e) {
22             e.printStackTrace();
23         }
24         
25         return retStr;
26     }

 

 1 /**
 2      * 从微信服务器取回菜单详细信息
 3      * @param accessToken 微信服务器访问所需accessToken
 4      * @return 菜单详细信息
 5      */
 6     public static String getMenuInfoFromQQ(String accessToken) {
 7         String retStr = "";
 8         
 9         try {
10             String baseUrl = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken;
11             
12             HttpClient httpclient = new DefaultHttpClient();
13             HttpGet httpget = new HttpGet(baseUrl);
14             HttpResponse response = httpclient.execute(httpget);
15             retStr = EntityUtils.toString(response.getEntity(), "UTF-8");
16         } catch (Exception e) {
17             e.printStackTrace();
18         }
19          return retStr;
20     }
 1 /**
 2      * 创建腾讯服务器的菜单信息
 3      * @param accessToken 微信服务器访问所需accessToken
 4      * @param menuInfoStr 菜单信息
 5      * @return true:创建成功<br/>
 6      *        false:创建失败
 7      */
 8     public static boolean createMenuInfo2QQ(String accessToken, String menuInfoStr) {
 9         boolean retBool = false;
10         
11         try {
12             String baseUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
13             
14             HttpClient httpclient = new DefaultHttpClient();
15             HttpPost httppost = new HttpPost(baseUrl);
16             httppost.setEntity(new StringEntity(menuInfoStr, HTTP.UTF_8));
17             HttpResponse response = httpclient.execute(httppost);
18             String wxResultStr = EntityUtils.toString(response.getEntity());
19             // 返回的数据类似【{"errcode":0,"errmsg":"ok"}】
20             JSONObject objRoot = new JSONObject(wxResultStr);
21             if ("ok".equalsIgnoreCase(objRoot.getString("errmsg"))) {
22                 retBool = true;
23             }
24         } catch (Exception e) {
25             e.printStackTrace();
26         }
27         
28         return retBool;
29     }

 

 1 /**
 2      * 删除腾讯服务器的菜单信息
 3      * @param accessToken 微信服务器访问所需accessToken
 4      * @return true:删除成功<br/>
 5      *        false:删除失败
 6      */
 7     public static boolean delMenuInfoFromQQ(String accessToken) {
 8         boolean retBool = false;
 9         
10         try {
11             String baseUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken;
12             
13             HttpClient httpclient = new DefaultHttpClient();
14             HttpGet httpget = new HttpGet(baseUrl);
15             HttpResponse response = httpclient.execute(httpget);
16             String wxResultStr = EntityUtils.toString(response.getEntity());
17             // 返回的数据类似【{"errcode":0,"errmsg":"ok"}】
18             JSONObject objRoot = new JSONObject(wxResultStr);
19             if ("ok".equalsIgnoreCase(objRoot.getString("errmsg"))) {
20                 retBool = true;
21             }
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25         
26         return retBool;
27     }
28     

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!