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