本来最开始研究的腾讯微博第三方登录,但腾讯微博的SDK太糟糕了,估计他们自己都放弃了。想想也是,除了腾讯自家的应用在用腾讯微博,其他的开发者基本不去碰这块。所以马上去研究微博老大去了,平台果然强大,代码和文档一大堆,研究了半天,找点自己需要的功能,还是有点收获,先把这些记录下来,以后再慢慢研究。
目前实现的是实现第三方的登录,获取用户名显示到第三方应用上面。
主类,用到了Afinal框架
public class WbMainAct extends FinalActivity {
// @ViewInject(id = R.id.login_out_button_silver, click = "myClick")
LoginoutButton oauthButton;
// @ViewInject(id = R.id.mytv)
TextView textView;
AuthInfo authInfo;
@ViewInject(id = R.id.user, click = "getUser")
Button userButton;
@ViewInject(id = R.id.userInfo)
TextView userTextView;
UsersAPI userAPI;
private Oauth2AccessToken mAccessToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.oauthlay);
authInfo = new AuthInfo(getApplicationContext(), Constants.APP_KEY,
Constants.REDIRECT_URL, Constants.SCOPE);
oauthButton = (LoginoutButton) findViewById(R.id.login_out_button_default);
textView = (TextView) findViewById(R.id.mytv);
oauthButton.setWeiboAuthInfo(authInfo, new AuthListener());
oauthButton.setLogoutListener(new LogOutRequestListener());
final Button logoutButton = (Button) findViewById(R.id.logout_button);
logoutButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(WbMainAct.this, "onClick", Toast.LENGTH_SHORT)
.show();
new LogoutAPI(WbMainAct.this, Constants.APP_KEY,
AccessTokenKeeper.readAccessToken(WbMainAct.this))
.logout(new LogOutRequestListener());
}
});
}
public void getUser(View v) {
mAccessToken = AccessTokenKeeper.readAccessToken(this);
userAPI = new UsersAPI(this, Constants.APP_KEY, mAccessToken);
if (mAccessToken != null && mAccessToken.isSessionValid()) {
long uid = Long.parseLong(mAccessToken.getUid());
userAPI.show(uid, mListener);
} else {
Toast.makeText(WbMainAct.this,
R.string.weibosdk_demo_access_token_is_empty,
Toast.LENGTH_LONG).show();
userTextView
.setText((mAccessToken == null) ? "mAccessToken = null "
+ mAccessToken.isSessionValid()
: "mAccessToken != null "
+ mAccessToken.isSessionValid());
}
}
private RequestListener mListener = new RequestListener() {
@Override
public void onComplete(String response) {
if (!TextUtils.isEmpty(response)) {
// 调用 User#parse 将JSON串解析成User对象
User user = User.parse(response);
if (user != null) {
Toast.makeText(WbMainAct.this,
"获取User信息成功,用户昵称:" + user.screen_name,
Toast.LENGTH_LONG).show();
userTextView.setText(user.screen_name);
} else {
userTextView.setText(response);
Toast.makeText(WbMainAct.this, "2:" + response,
Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onWeiboException(WeiboException e) {
ErrorInfo info = ErrorInfo.parse(e.getMessage());
Toast.makeText(WbMainAct.this, "3:" + info.toString(),
Toast.LENGTH_LONG).show();
userTextView.setText(info.toString());
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//
if (oauthButton != null) {
oauthButton.onActivityResult(requestCode, resultCode, data);
}
}
private class LogOutRequestListener implements RequestListener {
@Override
public void onComplete(String response) {
if (!TextUtils.isEmpty(response)) {
try {
JSONObject obj = new JSONObject(response);
String value = obj.getString("result");
if ("true".equalsIgnoreCase(value)) {
AccessTokenKeeper.clear(WbMainAct.this);
textView.setText(R.string.weibosdk_demo_logout_success);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onWeiboException(WeiboException e) {
textView.setText(R.string.weibosdk_demo_logout_failed);
}
}
private class AuthListener implements WeiboAuthListener {
@Override
public void onComplete(Bundle values) {
Toast.makeText(WbMainAct.this, "onComplete", Toast.LENGTH_SHORT)
.show();
Oauth2AccessToken accessToken = Oauth2AccessToken
.parseAccessToken(values);
if (accessToken != null && accessToken.isSessionValid()) {
String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
.format(new java.util.Date(accessToken.getExpiresTime()));
String format = getString(R.string.weibosdk_demo_token_to_string_format_1);
textView.setText(String.format(format, accessToken.getToken(),
date));
AccessTokenKeeper.writeAccessToken(getApplicationContext(),
accessToken);
}
}
@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(WbMainAct.this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
@Override
public void onCancel() {
Toast.makeText(WbMainAct.this,
R.string.weibosdk_demo_toast_auth_canceled,
Toast.LENGTH_SHORT).show();
}
}
}
常量类,根据实际情况替换相关数据
public interface Constants {
public static final String APP_KEY = "1530318928";
public static final String REDIRECT_URL = "http://open.weibo.com/apps/1530318928/info/advanced";
public static final String SCOPE = "email,direct_messages_read,direct_messages_write,"
+ "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
+ "follow_app_official_microblog," + "invitation_write";
}工具类,保持从服务器返回的token信息
public class AccessTokenKeeper {
private static final String PREFERENCES_NAME = "com_weibo_sdk_android";
private static final String KEY_UID = "uid";
private static final String KEY_ACCESS_TOKEN = "access_token";
private static final String KEY_EXPIRES_IN = "expires_in";
/**
* 保存 Token 对象到 SharedPreferences。
*
* @param context 应用程序上下文环境
* @param token Token 对象
*/
public static void writeAccessToken(Context context, Oauth2AccessToken token) {
if (null == context || null == token) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(KEY_UID, token.getUid());
editor.putString(KEY_ACCESS_TOKEN, token.getToken());
editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
editor.commit();
}
/**
* 从 SharedPreferences 读取 Token 信息。
*
* @param context 应用程序上下文环境
*
* @return 返回 Token 对象
*/
public static Oauth2AccessToken readAccessToken(Context context) {
if (null == context) {
return null;
}
Oauth2AccessToken token = new Oauth2AccessToken();
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
token.setUid(pref.getString(KEY_UID, ""));
token.setToken(pref.getString(KEY_ACCESS_TOKEN, ""));
token.setExpiresTime(pref.getLong(KEY_EXPIRES_IN, 0));
return token;
}
/**
* 清空 SharedPreferences 中 Token信息。
*
* @param context 应用程序上下文环境
*/
public static void clear(Context context) {
if (null == context) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.clear();
editor.commit();
}
}
学习笔记—— 微信5.x 界面
看了这样一个视频课程:http://www.imooc.com/learn/198之前从未觉得,编写界面哪里有趣,最近尝试了一下,还是挺好玩的。之前写过的多数应用都是最基本
Msm8960(APQ8064)平台的MSM-AOSP-kitkat编译适配(2):开发环境及相关的使用技巧
因为面对的是新手,这篇文章可能讲得比较宽泛,包括了硬件的选用,系统的选用,是否该用虚拟机,如何配置linux,linux的一些使用技巧,调试环境的
android应用的界面编程
android应用的界面编程第一组UI组件:布局管理器线性布局LinearLayout表格布局TableLayout帧布局FrameLayout相对布局RelativeLayout网格布局GridLayout绝对布局AbsoluteLa