• 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 
  • 相关阅读:
    【Eolinker使用】接口测试--如何解决接口重定向
    ExtJS按钮
    Redis-消费模式
    Redis笔记教程
    C++中this指针的用法
    C — 对C语言的认识
    你还在迷茫什么
    2019-2020-1 20199324《Linux内核原理与分析》第四周作业
    2019-2020-1 20199324《Linux内核原理与分析》第三周作业
    2019-2020-1 20199324《Linux内核原理与分析》第二周作业
  • 原文地址:https://www.cnblogs.com/clarence/p/3675798.html
Copyright © 2020-2023  润新知