Android新浪微博登陆和获取个人信息

      自己总结的一些android新浪登陆和根据用户ID获取用户信息,以下代码只是写了重要的部分,详细代码可到新浪官方下载一个Demo,里面都有详细流程和各种API。

Constant.java   常量类

View Code
/** 新浪设置appkey及appsecret access_token */
public static final String CONSUMER_KEY = "XXXXXXXX";
public static final String CONSUMER_SECRET = "XXXXXXXXXXXX";
public static String ACCESS_TOKEN = "";

新浪微博登陆事件

View Code
        tv_sina.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            try {
                Weibo weibo = Weibo.getInstance();
                weibo.setupConsumerConfig(Constant.CONSUMER_KEY, Constant.CONSUMER_SECRET);
                // 隐式授权认证方式
                weibo.setRedirectUrl("http://XXXXX.com");
                weibo.authorize(LoginActivity.this,new AuthDialogListener()); 
                } catch (Exception e) {}
            }
        });

    /** 新浪微博认证登陆框 */
    class AuthDialogListener implements WeiboDialogListener {
        @Override
        public void onComplete(Bundle values) {
            
            // 清楚新浪微博API登陆账号密码
            Utility.clearCookies(LoginActivity.this);
            Utility.clearRequestHeader();
            CookieManager.getInstance().removeSessionCookie();
            
            token = values.getString("access_token");
            String expires_in = values.getString("expires_in");
            wb_uid = values.getString("uid");
            // 保存常量
            Constant.ACCESS_TOKEN = token;
            Utility.setAuthorization(new Oauth2AccessTokenHeader());
            AccessToken accesstoken = new AccessToken(token, Constant.CONSUMER_SECRET);
            accesstoken.setExpiresIn(expires_in);
            Weibo.getInstance().setAccessToken(accesstoken);
            /*
            synchronized (LoginActivity.class) {
                if(sendhandler == false){
                    sendhandler = true;
                    Log.i("LOGIN", "********************************");
                    Message msg = Message.obtain();
                    msg.arg1 = 1;
                    handler.sendMessage(msg);
                }
                sendhandler = true;
            }
            */
        }
        @Override
        public void onError(DialogError e) {
        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onWeiboException(WeiboException e) {
        }
    }

根据用户ID获取用户信息

View Code
    /**
     * 新浪微博:根据用户ID获取用户信息
     * @param weibo
     * @return
     * @throws MalformedURLException
     * @throws IOException
     * @throws WeiboException
     */
    public String getUserCounts(Weibo weibo,String uid) 
            throws MalformedURLException, IOException, WeiboException {
        String url = Weibo.SERVER + "users/show.json";
        WeiboParameters bundle = new WeiboParameters();
        bundle.add("source", Weibo.getAppKey());
        bundle.add("uid", uid);
        String res = weibo.request(this, url, bundle, "GET", weibo.getAccessToken());
        return res;
    }

    /** 通过JSON获取到新浪微博用户信息,且解析相关内容信息 */
    String UserResult = getUserCounts(Weibo.getInstance(), wb_uid);
    JSONObject jsonObj = new JSONObject(UserResult);
    // 用户昵称
    wb_screen_name = jsonObj.getString("screen_name");
    // 友好显示名称(name)
    wb_name = jsonObj.getString("name");
    // 用户头像地址,50×50像素
    wb_profile_image_url = jsonObj.getString("profile_image_url");

 

 

 

 

 

 

 

 

 

 

posted on 2013-03-21 10:59  大米稀饭  阅读(702)  评论(0编辑  收藏  举报