先看微博的整体样子,有了一定的轮廓再分包,大致可分为四个包:一个专门显示界面的Activity包,一个net包(主要包括对网络的一些操作,比如:获取所有的好友信息,评论信息等),工具包Tookit(主要对一些常见的公共操作如:日期和String的相互转换),还有一个服务包(主要对一些适配器adapter,监听器等的操作)
开发之前我们必须在新浪开放平台http://open.weibo.com/development/申请一个key,具体操作就不说,网上有一大把的图片教程,很简单的。后面我们都是通过这个key来拿新浪服务器数据的。有了这个key后,我们还需要下载一个新浪已经开发好的项目sina_oauth2.0(觉得难找的可以看我csdn资源进行下载,同时我也把图片资源也一起打包发过去,待会就上传),它相当于一个帮助类,我们进行Oauth2.0认证时,或者通过新浪API接口获取数据时,它里面都封装了一些常用的方法,所以我们下载了这个项目后就把它import工作空间同时把它作为lib。
好了有了前面的基础知识,我们来看下效果图:
登录页面很简单一个的实现ImageView 一个对话框 下面是登录界面(LoginActivity):
public class LoginActivity extends Activity{
private boolean b ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login) ;
//开一个新的线程 去验证信息
// new Thread(new Runnable() {
//
// public void run() {
//
// Log.v("otcyan", b+"--->") ;
// }
// }) ;
// //开一个handler 去更新 界面 的元素
// Handler handler = new Handler(){
// public void handleMessage(Message msg) {
// if(msg.what==LoginActivity.MSG){
//
// ++count ;
// if(count==3){
// loding.setText(R.string.loding) ;
// count = 0 ;
// }else {
// for (int i = 0; i < count; i++) {
// sb.append(".") ;
// }
// loding.setText(loding.getText().toString()+sb.toString()) ;
// }
// }
// };
// } ;
//loding(handler);
Oauth oauth = new Oauth(LoginActivity.this) ;
Utility.setAuthorization(new Oauth2AccessTokenHeader()) ;
b = oauth.login() ;
Log.v("otcyan", b+"") ;
if(b){
Log.v("otcyan", "entry") ;
//已经授权 数据 已经准备完毕 启动mainactivity进行数据操作
Intent intent = new Intent(this, MainActivity.class) ;
startActivity(intent) ;
finish() ;
}else{
Log.v("otcyan", "no entry") ;
//启动另外一个activity进行授权
Intent intent = new Intent(this,DialogActivity.class) ;
startActivity(intent) ;
finish() ;
}
}
}
这个界面的布局文件如下(布局文件没写好 大家不必参考我的,这只是一个笔记我把它贴出来了):
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/splash_background"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ver"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/loding"
android:text="@string/loding"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:textSize="24dp"
/>
<ImageView
android:id="@+id/authorimageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/author"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
下面是一个授权的界面(DialogActivity.java)
package com.otcyan.weibo.activity;
import com.otcyan.weibo.net.MyWeiboDialogListener;
import com.otcyan.weibo.net.OtWeibo;
import com.weibo.net.Weibo;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class DialogActivity extends Activity{
private Dialog dialog = null ;
private Button bn = null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog) ;
//添加一个dialog
View diaView=View.inflate(this, R.layout.logindialog, null);
dialog=new Dialog(DialogActivity.this,R.style.dialog);
dialog.setContentView(diaView);
dialog.show();
bn = (Button) diaView.findViewById(R.id.start) ;
Log.v("otcyan", "bn------------------->"+bn) ;
bn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//当点击 开始的时候 进行授权 操作
Weibo weibo = Weibo.getInstance() ;
weibo.setupConsumerConfig(OtWeibo.CONSUMER_KEY, OtWeibo.CONSUMER_SECRET) ;
weibo.setRedirectUrl(OtWeibo.SIAN_CALLBACK) ;
weibo.authorize(DialogActivity.this, new MyWeiboDialogListener(DialogActivity.this)) ;
}
}) ;
}
}
布局文件如下:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/cmcc_splash_background"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="@drawable/ver1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
/>
</LinearLayout>
以下是当点击登录时候的Oauth2.0授权:
package com.otcyan.weibo.net;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.otcyan.weibo.activity.MainActivity;
import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;
public class MyWeiboDialogListener implements WeiboDialogListener {
private Context context = null ;
public MyWeiboDialogListener(Context context){
this.context = context ;
}
public void onComplete(Bundle values) {
String token = values.getString("access_token");
String expires_in = values.getString("expires_in");
//获取这个用户的uid
String uid = values.getString("uid");
Log.v("otcyan", "uid---------->"+uid) ;
Toast.makeText(context, "授权成功!!获得token值:"+token, Toast.LENGTH_LONG).show() ;
//根据 token 构建一个AccessToken
AccessToken accessToken = new AccessToken(token,OtWeibo.CONSUMER_SECRET);
//到期时间
accessToken.setExpiresIn(expires_in);
Weibo.getInstance().setAccessToken(accessToken);
///保存到sharedpreference
SharedPreferences preferences = context.getSharedPreferences(OtWeibo.SHARED_OAUTH, 0) ;
Editor editor = preferences.edit() ;
editor.putString(OtWeibo.ACCESS_TOKEN, token);
editor.putString(OtWeibo.EXPIRES_IN, expires_in);
editor.putString(OtWeibo.UID, uid) ;
editor.commit() ;
//启动
Intent intent = new Intent();
intent.setClass(context, MainActivity.class);
context.startActivity(intent);
}
public void onWeiboException(WeiboException e) {
}
public void onError(DialogError e) {
}
public void onCancel() {
}
}
这样就完成了通过Oauth2.0对用户名跟密码的验证了。。很简单吧!!!
下一章讲记录主界面的思路与实现 对于