zoukankan      html  css  js  c++  java
  • 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 
  • 相关阅读:
    逆序对
    归并排序
    快速排序
    利用WPF InkCanvas水墨控件圈选移动笔画
    Vue使用websocket进行心跳连接
    socket + vue + canvas实践,你画我猜(一)
    Vue+WebSocket+ES6+Canvas 制作【你画我猜】小游戏
    HTML5 canvas实现画板功能
    Canvas手工作图并实时同步复现(利用websocket)
    什么是SoC?
  • 原文地址:https://www.cnblogs.com/clarence/p/3675798.html
Copyright © 2011-2022 走看看