此部分为进行新浪微博的oauth2.0的授权,截图肯能有些问题,应该是个对话框的形式,在此说明一下,可能代码里调用一些方法在本篇中没有,但是全部在我的代码里,也不要索取图片,要看全部代码的请移步http://blog.csdn.net/gaojin1991/article/details/8022213
<?xml version="1.0" encoding="utf-8"?>
<!-- 该布局文件为authorizeActivity的布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background"
>
</LinearLayout>

package com.czu.sinaweibo;
import java.io.IOException;
import java.net.MalformedURLException;
import com.czu.constdata.ConstDataOAuth;
import com.czu.constdata.ConstDataSinaAPI;
import com.czu.databasehandler.UserInfoHandler;
import com.czu.sinaweibo.R;
import com.czu.utils.Global;
import com.czu.utils.Utils;
import com.czu.weiboHandler.WeiBoInfoPraser;
import com.czu.weiboadapter.UserInfo;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class AuthorizeActivity extends Activity {
/* 此地址对于移动应用来说没有意义,只需要与我在新浪填写的地址一致即可 */
private static final String callBackURL = "http://www.renren.com/313128238";
Weibo mWeibo = Weibo.getInstance();
private Dialog myDialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 设置没有标题栏 */
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.authorize_activity);
/* 定义视图样式 */
View diaView = View.inflate(this, R.layout.authorize_dialog, null);
/* 新建一个dialog对象,并且定义了主题 */
myDialog = new Dialog(AuthorizeActivity.this, R.style.dialog);
/* 将试图内容显示在dialog中 */
myDialog.setContentView(diaView);
/* 显示dialog */
myDialog.show();
Button startButton = (Button) diaView.findViewById(R.id.btn_start);
startButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// System.out.println("fetching request token from sina----->AuthorizeActivity");
if(Utils.hasInternet(AuthorizeActivity.this)){
myDialog.dismiss();
Weibo weibo = Weibo.getInstance();
weibo.setupConsumerConfig(ConstDataOAuth.APP_KEY,
ConstDataOAuth.APP_SECRET);
weibo.setRedirectUrl(callBackURL);
weibo.authorize(AuthorizeActivity.this,
new AuthDialogListener());
}
else {
Toast.makeText(AuthorizeActivity.this, "请检查您的网络连接!", Toast.LENGTH_SHORT).show();
Intent intent=new Intent("android.settings.SETTINGS");
startActivity(intent);
}
}
});
/* 取消按钮 */
Button cancelButton = (Button) diaView.findViewById(R.id.btn_cancel);
cancelButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
finish();
}
});
}
public void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
this.finish();
}
class AuthDialogListener implements WeiboDialogListener {
@Override
public void onComplete(Bundle values) {
String token = values.getString("access_token");
// String expires_in = values.getString("expires_in");
/* 获取用户的Id */
String userIdString = values.getString("uid");
// System.out.println("test--------------->"+userIdString);
UserInfo userInfo = new UserInfo();
/* 设置一下accesstoken */
userInfo.setAccessToken(token);
// System.out.println("Token:"+token);
Global.setCurrentUserInfo(userInfo);
WeiBoInfoPraser informationParser = new WeiBoInfoPraser(
AuthorizeActivity.this);
try {
userInfo = informationParser.getUserInfo(ConstDataSinaAPI.show,
userIdString);
/* 再次设置一下AccessToken,应为在调用上面的方法的时候新建了一个userInfo对象 */
userInfo.setAccessToken(token);
Global.setCurrentUserInfo(userInfo);
// System.out.println("get user id:"+Global.getCurrentUserInfo().getUserId());
/** 下载当前用户头像,并且保存在currentuserInfo中 */
// System.out.println("get user name:"+Global.getCurrentUserInfo().getUserName());
// System.out.println("get access token:"+Global.getCurrentUserInfo().getAccessToken());
// System.out.println("get user icon:"+Global.getCurrentUserInfo().getUserIconString());
/* 将获取的用户ID和AccessToken保存在数据库中 */
UserInfoHandler userInfoHandler = new UserInfoHandler(
AuthorizeActivity.this);
if (userInfoHandler.HaveUserInfo(userIdString)) {
userInfoHandler.UpdateUserInfo(Global.getCurrentUserInfo());
} else {
userInfoHandler.SaveUserInfo(Global.getCurrentUserInfo());
}
/* 关闭数据库 */
userInfoHandler.CloseDataBase();
/*
* AccessToken accessToken = new AccessToken(token,
* ConstDataOAuth.APP_SECRET);
* accessToken.setExpiresIn(expires_in);
*
* Weibo.getInstance().setAccessToken(accessToken);
*/
Intent intent = new Intent();
intent.setClass(AuthorizeActivity.this, MainActivity.class);
startActivity(intent);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (WeiboException e) {
e.printStackTrace();
Toast.makeText(AuthorizeActivity.this,
"糟糕,出现错误了:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onError(DialogError e) {
Toast.makeText(getApplicationContext(),
"Oauth认证错误:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Oauth认证取消",
Toast.LENGTH_LONG).show();
}
@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(getApplicationContext(),
"Oauth认证异常" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}

855

被折叠的 条评论
为什么被折叠?



