Project

Profile

Help

HostedRedmine.com has moved to the Planio platform. All logins and passwords remained the same. All users will be able to login and use Redmine just as before. Read more...

Task #618662

closed

QQ在线登录实现

Added by 子琛 郝 about 8 years ago. Updated almost 8 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Sprint/Milestone:
-
Start date:
2016-12-06
Due date:
2016-12-11
% Done:

100%

Estimated time:

Files

activity_main.xml (1.04 KB) activity_main.xml 洪磊 张, 2016-12-08 03:18 PM
AppConstant.java (167 Bytes) AppConstant.java 洪磊 张, 2016-12-08 03:18 PM
MainActivity.java (8.62 KB) MainActivity.java 洪磊 张, 2016-12-08 03:18 PM
Util.java (1.13 KB) Util.java 洪磊 张, 2016-12-08 03:18 PM
Actions #1

Updated by 洪磊 张 about 8 years ago

  • % Done changed from 0 to 40

package com.example.administrator.missphoto;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;

import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

import static android.app.Activity.RESULT_OK;

public class MineFragment extends Fragment {
@Nullable
private Context context;
private View view;
private ImageButton btn_mine_qq;
private ImageButton btn_mine_weibo;
private ImageButton btn_mine_phone;
private Tencent mTencent;
private IUiListener loginListener;
private QQToken qqToken;
private UserInfo userInfo;
private TextView tvNickName;
private ImageView headerLogo;

public void setContext(Context context) {
this.context = context;
}
@Override
public Context getContext() {
return context;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.layout_mine, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
findView();
btn_mine_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(getActivity(), ZhuceActivity.class);
startActivity(intent);
}
});
btn_mine_qq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//实例化mTencent
mTencent = Tencent.createInstance("1105792889",getActivity());
//登录,第二个参数为实现IUiListener接口
mTencent.login(getActivity(),"all",loginListener);
qqToken=mTencent.getQQToken();
userInfo=new UserInfo(getActivity(),qqToken);
userInfo.getUserInfo(new IUiListener() {//异步方式获取用户信息
@Override
public void onComplete(final Object o) {
JSONObject userInfoJson = (JSONObject) o;
Message msgNick = new Message();
msgNick.what = 0;//昵称
try {
msgNick.obj = userInfoJson.getString("nickname");//直接传递一个昵称的内容过去
} catch (Exception e) {
e.printStackTrace();
}
mHandler.sendMessage(msgNick);
//子线程 获取并传递头像图片,由Handler更新
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bitmapHead = null;
if (((JSONObject)o).has("figureurl")) {
try {
String headUrl = ((JSONObject) o).getString("figureurl_qq_2");
bitmapHead = Util.getbitmap(headUrl);
} catch (JSONException e) {
e.printStackTrace();
}
Message msgHead = new Message();
msgHead.what = 1;
msgHead.obj = bitmapHead;
mHandler.sendMessage(msgHead);
}
}
}).start();
}
@Override
public void onError(UiError uiError) {
System.out.print(uiError.toString());
Log.e("GET_QQ_INFO_ERROR", "获取qq用户信息错误");
Toast.makeText(getActivity(), "获取qq用户信息错误", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
System.out.print("cancel");
Log.e("GET_QQ_INFO_CANCEL", "获取qq用户信息取消");
Toast.makeText(getActivity(), "获取qq用户信息取消", Toast.LENGTH_SHORT).show();
}
});
//监听实现
loginListener = new IUiListener() {
@Override
public void onComplete(Object o) {
if(o==null){
Toast.makeText(getActivity(),"返回结果为空",Toast.LENGTH_LONG).show();
return;
}
//objResp= {"ret":0,"openid":"E783DBFBE70A0C83560F2153933AA7BE","access_token":"6D0416C27ED96E5570B1AEEA1250CEF5","pay_token":"308D2AB68D052081832B4AFEB07F86D1","expires_in":7776000,"pf":"desktop_m_qq-10000144-android-2002-","pfkey":"f05741cf1cad3a97be1fc337f3ff1913","msg":"","login_cost":525,"query_authority_cost":335,"authority_cost":0}
JSONObject jo= (JSONObject) o;
if(null!=jo&&jo.length()==0){
Toast.makeText(getActivity(),"返回结果为空",Toast.LENGTH_LONG).show();
}
Toast.makeText(getActivity(),"登录成功",Toast.LENGTH_LONG).show();
//处理结果
System.out.println(jo.toString());
try {
int ret=jo.getInt("ret");
if(ret==0){
Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_LONG).show();
String openID = jo.getString("openid");
String accessToken = jo.getString("access_token");
String expires = jo.getString("expires_in");
//下面两个方法非常重要,否则会出现client request's parameters are invalid, invalid openid
mTencent.setOpenId(openID);
mTencent.setAccessToken(accessToken, expires);
}
} catch (JSONException e) {
e.printStackTrace();
}
// handleQQLoginReceivedData(jsonvalues);
}
@Override
public void onError(UiError uiError) {
Toast.makeText(getActivity(),"登录错误",Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(getActivity(),"登录取消",Toast.LENGTH_LONG).show();
}
};
}
});
btn_mine_weibo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(getActivity(), ZhuceActivity.class);
startActivity(intent);
}
});
}
private void findView() {
btn_mine_qq=(ImageButton)view.findViewById(R.id.btn_mine_qq);
btn_mine_weibo=(ImageButton)view.findViewById(R.id.btn_mine_weibo);
btn_mine_phone=(ImageButton)view.findViewById(R.id.btn_mine_phone);
tvNickName = (TextView)view.findViewById(R.id.TvQQloginName);
headerLogo = (ImageView)view.findViewById(R.id.IvQQloginHeaderLogo);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//由于在一些低端机器上,因为内存原因,无法返回到回调onComplete里面,是以onActivityResult的方式返回
if(requestCode==11101&&resultCode==RESULT_OK){
//处理返回的数据
if(data==null){
Toast.makeText(getActivity(),"返回数据为空",Toast.LENGTH_LONG);
}else{
Tencent.handleResultData(data,loginListener);
}
}
}
//显示获取到的头像和昵称
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {//获取昵称
// JSONObject jo = (JSONObject)msg.obj;
// if (jo.has("nickname")){
// tvNickName.setText("登录成功\n" + "用户id"+openid+"\n昵称:"+response.getString("nickname")+"\n头像地址:"+response.get("figureurl_qq_1") + (CharSequence) msg.obj);
// }
tvNickName.setText((CharSequence) msg.obj);
} else if (msg.what == 1) {//获取头像
headerLogo.setImageBitmap((Bitmap) msg.obj);
}
}
};
}
Actions #2

Updated by 洪磊 张 almost 8 years ago

Actions #3

Updated by 洪磊 张 almost 8 years ago

  • % Done changed from 80 to 100
Actions #5

Updated by 洪磊 张 almost 8 years ago

  • Status changed from New to Closed

Also available in: Atom PDF