• QQ登入(2)获取用户信息


    private void initView() {
    mUserInfo = (TextView) findViewById(R.id.user_info);
    mUserLogo = (ImageView) findViewById(R.id.user_logo);
    }

    Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
    if (msg.what == 0) {
    JSONObject response = (JSONObject) msg.obj;
    if (response.has("nickname")) {
    try {
    mUserInfo.setText(response.toString());
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }else if(msg.what == 1){
    Bitmap bitmap = (Bitmap)msg.obj;
    mUserLogo.setImageBitmap(bitmap);
    }
    }
    };
    private void getUserInfo() {
    if (mQQAuth != null && mQQAuth.isSessionValid()) {
    IUiListener listener = new IUiListener() {
    @Override
    public void onError(UiError e) {
    Toast.makeText(getApplicationContext(), "获取用户信息失败", 0).show();
    }
    @Override
    public void onComplete(final Object response) {
    Message msg = new Message();
    msg.obj = response;
    msg.what = 0;
    mHandler.sendMessage(msg);
    new Thread(){
    @Override
    public void run() {
    JSONObject json = (JSONObject)response;
    if(json.has("figureurl")){
    Bitmap bitmap = null;
    try {
    bitmap = Tool.getbitmap(json.getString("figureurl_qq_2"));
    } catch (JSONException e) {

    }
    Message msg = new Message();
    msg.obj = bitmap;
    msg.what = 1;
    mHandler.sendMessage(msg);
    }
    }

    }.start();
    }

    @Override
    public void onCancel() {
    Toast.makeText(getApplicationContext(), "获取用户信息取消", 0).show();
    }
    };
    mInfo = new UserInfo(this, mQQAuth.getQQToken());
    mInfo.getUserInfo(listener);
    }
    }

    获取头像工具:

    package sdkjfs.e;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.util.Log;
    
    public class Tool {
    
        /**
         * 根据一个网络连接(String)获取bitmap图像
         * 
         * @param imageUri
         * @return
         * @throws MalformedURLException
         */
        public static Bitmap getbitmap(String imageUri) {
            // 显示网络上的图片
            Bitmap bitmap = null;
            try {
                URL myFileUrl = new URL(imageUri);
                HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                bitmap = BitmapFactory.decodeStream(is);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            return bitmap;
        }
    }
     源码:链接: http://pan.baidu.com/s/1qWwJQ24 
  • 相关阅读:
    jquery 将一组元素转换成数组
    QT5之三大重要窗体
    OSG消息传递机制分析
    OSG消息机制之消息分析
    OSG消息机制之事件处理概述
    OSG 3D场景渲染编程概述
    数据结构之中缀表达式转为后缀
    数据结构之小括号匹配
    考研复习策略
    数据结构之图
  • 原文地址:https://www.cnblogs.com/clarence/p/3675798.html
Copyright © 2020-2023  润新知