zoukankan      html  css  js  c++  java
  • 微信公众号获取用户信息工具类

    package com.hfcx.cfms.base.utils;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import com.hfcx.cfms.manage.entity.WxUser;
    
    import net.sf.json.JSONObject;
    
    public class WeixinUtil {
        
        private static String proxyHost = null;
        private static Integer proxyPort = null;
        
        public static final String APPID = "";
        public static final String SECRET = "";
    
        public static final String token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APPID+"&secret="+SECRET+"&grant_type=authorization_code";
        
        public static JSONObject httpsRequest(String code) throws Exception{
            String requestURL = token_url +"&code="+code;
            //第一次请求 获取access_token 和 openid
            String openid = doGet(requestURL);
            JSONObject jo = JSONObject.fromObject(openid);
            return jo;
        }
        
        private static String doGet(String url) throws Exception {
            URL localURL = new URL(url);
            URLConnection connection = openConnection(localURL);
            HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
            httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            InputStream inputStream = null;
            InputStreamReader inputStreamReader = null;
            BufferedReader reader = null;
            StringBuffer resultBuffer = new StringBuffer();
            String tempLine = null;
            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }
            try {
                inputStream = httpURLConnection.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
                reader = new BufferedReader(inputStreamReader);
                while ((tempLine = reader.readLine()) != null) {
                    resultBuffer.append(tempLine);
                }
            } finally {
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            }
            return resultBuffer.toString();
        }
        private static URLConnection openConnection(URL localURL) throws IOException {
            URLConnection connection;
            if (proxyHost != null && proxyPort != null) {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                connection = localURL.openConnection(proxy);
            } else {
                connection = localURL.openConnection();
            }
            return connection;
        }
        
        public static WxUser getUserInfo(JSONObject jsonObject){
            WxUser wxUser = new WxUser();
            wxUser.setOpenId(jsonObject.getString("openid"));
            wxUser.setSex(jsonObject.getInt("sex"));
            wxUser.setCity(jsonObject.getString("city"));
            wxUser.setLanguage(jsonObject.getString("language"));
            //过滤emoji
            wxUser.setNickname(filterEmoji(jsonObject.getString("nickname")));
            wxUser.setHeadImgUrl(jsonObject.getString("headimgurl"));
            wxUser.setProvince(jsonObject.getString("province"));
            wxUser.setCountry(jsonObject.getString("country"));
            
            return wxUser;
        }
        
        public static String filterEmoji(String source) {
            if (source == null) {
                return source;
            }
            Pattern emoji = Pattern.compile("[ud83cudc00-ud83cudfff]|[ud83dudc00-ud83dudfff]|[u2600-u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find()) {
                source = emojiMatcher.replaceAll("*");
                return source;
            }
            return source;
        }
    }

     其中用户信息实体类

    package com.hfcx.cfms.manage.entity;
    
    public class WxUser {
        // 用户的标识
        private String openId;
        // 昵称
        private String nickname;
        // 用户的性别(1是男性,2是女性,0是未知)
        private int sex;
        // 用户所在国家
        private String country;
        // 用户所在省份
        private String province;
        // 用户所在城市
        private String city;
        // 用户的语言,简体中文为zh_CN
        private String language;
        // 用户头像
        private String headImgUrl;
    
        public String getOpenId() {
            return openId;
        }
    
        public void setOpenId(String openId) {
            this.openId = openId;
        }
    
        public String getNickname() {
            return nickname;
        }
    
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
    
        public int getSex() {
            return sex;
        }
    
        public void setSex(int sex) {
            this.sex = sex;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        public String getProvince() {
            return province;
        }
    
        public void setProvince(String province) {
            this.province = province;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getLanguage() {
            return language;
        }
    
        public void setLanguage(String language) {
            this.language = language;
        }
    
        public String getHeadImgUrl() {
            return headImgUrl;
        }
    
        public void setHeadImgUrl(String headImgUrl) {
            this.headImgUrl = headImgUrl;
        }
    }
  • 相关阅读:
    032 Gradle 下载的依赖jar包在哪?
    031 can't rename root module,Android Studio修改项目名称
    030 Cannot resolve symbol'R' 问题解决汇总大全
    029 Android Studio层级显示目录文件
    028 You are about to commit CRLF line separators to the Git repository.It is recommended to set the core. autocrlf Git attribute to true to avoid line separator issues If you choose Fix and Comit ,
    027 【Android基础知识】Android Studio 编译慢及 Adb connection Error:远程主机强迫关闭了一个现有的连接
    026 Android Studio 和Gradle版版本对应关系
    025 Cause: org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierIm
    024 Android Studio上传项目到Github 最全记录
    023 解决AndroidStudio下载gradle慢的问题
  • 原文地址:https://www.cnblogs.com/zhengyuanyuan/p/10682617.html
Copyright © 2011-2022 走看看