QQ互联安卓官方SDK介绍连接:
http://wiki.connect.qq.com/android_sdk%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E能登陆成功,登陆成功的pay_token,pf,openid,expires_in,access_token这些都能在登录时正常返回,登录成功后开始尝试获取用户基本信息,比如QQ的OPENID,昵称,性别和头像等资料:
if (mTencent.isSessionValid()) {
mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO,
null, Constants.HTTP_GET, new BaseApiListener(
"get_simple_userinfo", true), null);}执行后 private void showResult(final String base, final String msg) { mHandler.post(new Runnable() { 这一句报错,求解?完整代码如下:import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;
import org.json.JSONObject;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import com.tencent.open.HttpStatusException;
import com.tencent.open.NetworkUnavailableException;
import com.tencent.tauth.Constants;
import com.tencent.tauth.IRequestListener;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;public class TestQQ extends Activity { private static final String APP_ID = "222222";
private Tencent mTencent;
private static final String SCOPE = "get_user_info,get_simple_userinfo,get_user_profile,get_app_friends,"
+ "add_share,add_topic,list_album,upload_pic,add_album,set_user_face,get_vip_info,get_vip_rich_info,get_intimate_friends_weibo,match_nick_tips_weibo"; private TextView mBaseMessageText;
private TextView mMessageText;
private Handler mHandler; private Button Btn_GetINF; @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.properties);
mTencent = Tencent.createInstance(APP_ID, this.getApplicationContext()); mBaseMessageText = (TextView) findViewById(R.id.base_message_tv);
mMessageText = (TextView) findViewById(R.id.message_tv); if (!mTencent.isSessionValid()) {
IUiListener listener = new BaseUiListener() {
@Override
protected void doComplete(JSONObject values) {
// updateLoginButton();
}
};
mTencent.login(this, SCOPE, listener);
} else {
mTencent.logout(this);
// updateLoginButton();
} Btn_GetINF = (Button) findViewById(R.id.button1);
Btn_GetINF.setOnClickListener(BtnClick); } private View.OnClickListener BtnClick = new View.OnClickListener() { public void onClick(View v) { if (v.getId() == Btn_GetINF.getId()) { if (mTencent.isSessionValid()) {
mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO,
null, Constants.HTTP_GET, new BaseApiListener(
"get_simple_userinfo", true), null); } } }
}; @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mTencent.onActivityResult(requestCode, resultCode, data);
} private class BaseUiListener implements IUiListener { @Override
public void onComplete(JSONObject response) { mBaseMessageText.setText("onComplete:"); mMessageText.setText(response.toString()); doComplete(response); } protected void doComplete(JSONObject values) { } @Override
public void onError(UiError e) { showResult("onError:", "code:" + e.errorCode + ", msg:" + e.errorMessage + ", detail:" + e.errorDetail); } @Override
public void onCancel() { showResult("onCancel", ""); } } private void showResult(final String base, final String msg) {
mHandler.post(new Runnable() { @Override
public void run() {
/*
* if (mProgressDialog.isShowing()) mProgressDialog.dismiss();
*/
mBaseMessageText.setText(base);
mMessageText.setText(msg);
}
});
} private class BaseApiListener implements IRequestListener {
private String mScope = "all";
private Boolean mNeedReAuth = false; public BaseApiListener(String scope, boolean needReAuth) {
mScope = scope;
mNeedReAuth = needReAuth;
} @Override
public void onComplete(final JSONObject response, Object state) {
showResult("IRequestListener.onComplete:", response.toString());
doComplete(response, state);
} protected void doComplete(JSONObject response, Object state) {
try {
int ret = response.getInt("ret");
if (ret == 100030) {
if (mNeedReAuth) {
Runnable r = new Runnable() {
public void run() {
mTencent.reAuth(TestQQ.this, mScope,
new BaseUiListener());
}
};
TestQQ.this.runOnUiThread(r);
}
} // azrael 2/1注释掉了, 这里为何要在api返回的时候设置token呢,
// 如果cgi返回的值没有token, 则会清空原来的token
/*
* String token = response.getString("access_token"); String
* expire = response.getString("expires_in"); String openid =
* response.getString("openid"); mTencent.setAccessToken(token,
* expire); mTencent.setOpenId(openid);
*/
} catch (JSONException e) {
e.printStackTrace();
Log.e("toddtest", response.toString());
} } @Override
public void onIOException(final IOException e, Object state) {
showResult("IRequestListener.onIOException:", e.getMessage());
} @Override
public void onMalformedURLException(final MalformedURLException e,
Object state) {
showResult("IRequestListener.onMalformedURLException", e.toString());
} @Override
public void onJSONException(final JSONException e, Object state) {
showResult("IRequestListener.onJSONException:", e.getMessage());
} @Override
public void onConnectTimeoutException(ConnectTimeoutException arg0,
Object arg1) {
showResult("IRequestListener.onConnectTimeoutException:",
arg0.getMessage()); } @Override
public void onSocketTimeoutException(SocketTimeoutException arg0,
Object arg1) {
showResult("IRequestListener.SocketTimeoutException:",
arg0.getMessage());
} @Override
public void onUnknowException(Exception arg0, Object arg1) {
showResult("IRequestListener.onUnknowException:", arg0.getMessage());
} @Override
public void onHttpStatusException(HttpStatusException arg0, Object arg1) {
showResult("IRequestListener.HttpStatusException:",
arg0.getMessage());
} @Override
public void onNetworkUnavailableException(
NetworkUnavailableException arg0, Object arg1) {
showResult("IRequestListener.onNetworkUnavailableException:",
arg0.getMessage());
}
}}说明:权限都没的说,肯定都配置到了。现在就是不知道为啥连腾讯官方提供的DEMO都编译后输入那个ID就出错,我这通过文档写出来的最基本的功能还能登陆成功,就是没法进行下一步操作。QQAndroidSDKQQ互联
http://wiki.connect.qq.com/android_sdk%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E能登陆成功,登陆成功的pay_token,pf,openid,expires_in,access_token这些都能在登录时正常返回,登录成功后开始尝试获取用户基本信息,比如QQ的OPENID,昵称,性别和头像等资料:
if (mTencent.isSessionValid()) {
mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO,
null, Constants.HTTP_GET, new BaseApiListener(
"get_simple_userinfo", true), null);}执行后 private void showResult(final String base, final String msg) { mHandler.post(new Runnable() { 这一句报错,求解?完整代码如下:import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;
import org.json.JSONObject;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import com.tencent.open.HttpStatusException;
import com.tencent.open.NetworkUnavailableException;
import com.tencent.tauth.Constants;
import com.tencent.tauth.IRequestListener;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;public class TestQQ extends Activity { private static final String APP_ID = "222222";
private Tencent mTencent;
private static final String SCOPE = "get_user_info,get_simple_userinfo,get_user_profile,get_app_friends,"
+ "add_share,add_topic,list_album,upload_pic,add_album,set_user_face,get_vip_info,get_vip_rich_info,get_intimate_friends_weibo,match_nick_tips_weibo"; private TextView mBaseMessageText;
private TextView mMessageText;
private Handler mHandler; private Button Btn_GetINF; @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.properties);
mTencent = Tencent.createInstance(APP_ID, this.getApplicationContext()); mBaseMessageText = (TextView) findViewById(R.id.base_message_tv);
mMessageText = (TextView) findViewById(R.id.message_tv); if (!mTencent.isSessionValid()) {
IUiListener listener = new BaseUiListener() {
@Override
protected void doComplete(JSONObject values) {
// updateLoginButton();
}
};
mTencent.login(this, SCOPE, listener);
} else {
mTencent.logout(this);
// updateLoginButton();
} Btn_GetINF = (Button) findViewById(R.id.button1);
Btn_GetINF.setOnClickListener(BtnClick); } private View.OnClickListener BtnClick = new View.OnClickListener() { public void onClick(View v) { if (v.getId() == Btn_GetINF.getId()) { if (mTencent.isSessionValid()) {
mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO,
null, Constants.HTTP_GET, new BaseApiListener(
"get_simple_userinfo", true), null); } } }
}; @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mTencent.onActivityResult(requestCode, resultCode, data);
} private class BaseUiListener implements IUiListener { @Override
public void onComplete(JSONObject response) { mBaseMessageText.setText("onComplete:"); mMessageText.setText(response.toString()); doComplete(response); } protected void doComplete(JSONObject values) { } @Override
public void onError(UiError e) { showResult("onError:", "code:" + e.errorCode + ", msg:" + e.errorMessage + ", detail:" + e.errorDetail); } @Override
public void onCancel() { showResult("onCancel", ""); } } private void showResult(final String base, final String msg) {
mHandler.post(new Runnable() { @Override
public void run() {
/*
* if (mProgressDialog.isShowing()) mProgressDialog.dismiss();
*/
mBaseMessageText.setText(base);
mMessageText.setText(msg);
}
});
} private class BaseApiListener implements IRequestListener {
private String mScope = "all";
private Boolean mNeedReAuth = false; public BaseApiListener(String scope, boolean needReAuth) {
mScope = scope;
mNeedReAuth = needReAuth;
} @Override
public void onComplete(final JSONObject response, Object state) {
showResult("IRequestListener.onComplete:", response.toString());
doComplete(response, state);
} protected void doComplete(JSONObject response, Object state) {
try {
int ret = response.getInt("ret");
if (ret == 100030) {
if (mNeedReAuth) {
Runnable r = new Runnable() {
public void run() {
mTencent.reAuth(TestQQ.this, mScope,
new BaseUiListener());
}
};
TestQQ.this.runOnUiThread(r);
}
} // azrael 2/1注释掉了, 这里为何要在api返回的时候设置token呢,
// 如果cgi返回的值没有token, 则会清空原来的token
/*
* String token = response.getString("access_token"); String
* expire = response.getString("expires_in"); String openid =
* response.getString("openid"); mTencent.setAccessToken(token,
* expire); mTencent.setOpenId(openid);
*/
} catch (JSONException e) {
e.printStackTrace();
Log.e("toddtest", response.toString());
} } @Override
public void onIOException(final IOException e, Object state) {
showResult("IRequestListener.onIOException:", e.getMessage());
} @Override
public void onMalformedURLException(final MalformedURLException e,
Object state) {
showResult("IRequestListener.onMalformedURLException", e.toString());
} @Override
public void onJSONException(final JSONException e, Object state) {
showResult("IRequestListener.onJSONException:", e.getMessage());
} @Override
public void onConnectTimeoutException(ConnectTimeoutException arg0,
Object arg1) {
showResult("IRequestListener.onConnectTimeoutException:",
arg0.getMessage()); } @Override
public void onSocketTimeoutException(SocketTimeoutException arg0,
Object arg1) {
showResult("IRequestListener.SocketTimeoutException:",
arg0.getMessage());
} @Override
public void onUnknowException(Exception arg0, Object arg1) {
showResult("IRequestListener.onUnknowException:", arg0.getMessage());
} @Override
public void onHttpStatusException(HttpStatusException arg0, Object arg1) {
showResult("IRequestListener.HttpStatusException:",
arg0.getMessage());
} @Override
public void onNetworkUnavailableException(
NetworkUnavailableException arg0, Object arg1) {
showResult("IRequestListener.onNetworkUnavailableException:",
arg0.getMessage());
}
}}说明:权限都没的说,肯定都配置到了。现在就是不知道为啥连腾讯官方提供的DEMO都编译后输入那个ID就出错,我这通过文档写出来的最基本的功能还能登陆成功,就是没法进行下一步操作。QQAndroidSDKQQ互联
解决方案 »
- Android4.0及以上版本的Activity切换为啥没有动画呢?
- [求助]android 如何获取滑动时的路径长度
- android 读取资源图片有大小的限制吗
- android和ios哪个好?
- android framework以及其他C++代码中的LOGV,如何输出?
- C8650 软件刷了花屏
- 百度地图计算路程问题
- eclipse中运行android 的程序 需要在run configurations->ApacheTomcat->argument怎么配置运行参数
- 请求webservice的时候报错误
- 手势识别 为啥不好用? 求指导
- Android Layout布局上的图片如何突破背景图
- 怎么搭个小服务器,练习下http使用
会不会是自己程序的问题。你就在doComplete打印输出看看先。
如果不是就换个版本的sdk。
我的问题也解决不了