安卓仿製新浪微博(一)之OAuth2受權接口

這裏須要用到請求受權(authorize)以及獲取受權(access_token)android

第一步:json

將新浪的sdk放在src/libs下面api

二:app

//建立方法實現authorize
public void getauthorize(){
//獲取實例
//實例化的方法通常有三種
//1.最多見的經過new;2.經過getinstence;3.經過工廠(Facetory)
Weibo weibo=Weibo.getInstence();//裏面有三個參數.String appKey, String redirectUrl, String aScope,
//分別是你應用的appKey,網址,想要獲取的權限能夠把這些屬性寫在一個類裏面設置成靜態
//這裏須要兩個參數,一個爲context,一個listenr
//context很好解決放的就是this,而listener須要的是WeiboAuthListener類型,咱們沒有全部就直接new一個
weibo.aututhorize(this,new WeiboAuthListener(){
//WeiboAuthListener裏面自動生成4個方法
//拋出異常時
            @Override
            public void onWeiboException(WeiboException arg0) {
                Log.d(TAG, "onWeiboException" + arg0);

            }

            // 出錯時
            @Override
            public void onError(WeiboDialogError arg0) {
                Log.d(TAG, "onError" + arg0);

            }

            // 成功時
            @Override
            public void onComplete(Bundle arg0) {//這裏的Bundle arg0就是返回的code數據
                Log.d(TAG, "onComplete" + arg0);
                String code = arg0.getString("code");
                //這是得到Toak的方法,在下面會寫到
                getToken(code);
            }

            // 取消時
            @Override
            public void onCancel() {
                Log.d(TAG, "onCancel=====");

            }

});
}

如今已經得到code,如今咱們須要經過code獲得Tokeneclipse

//實現access_token獲取已經受權的Access_token

public void getToken(){
//如今來填充參數
String url="https://api.weibo.com/oauth2/access_token";//注意這裏不能有空格
WeiboParameters params=new WeiboParameters();
//params的中文就是參數的意思,全部將參數放在裏面,所需的請求參數新浪API裏面都有http://open.weibo.com/wiki/OAuth2/access_token
params.add("client_id", staticname.AppKey);//第一個是key,第二個是value
params.add("client_secret", staticname.App_Secret);
params.add("grant_type", "authorization_code");
params.add("code", code);
params.add("redirect_uri", staticname.RedirectUrl);

//第一步先寫下面這個
AsyncWeiboRunner.request(url,params,"POST",new RequestListener(){
//自動生成四個方法
@Override
            public void onIOException(IOException arg0) {
                Log.d(TAG, "onIOException------" + arg0);

            }

            @Override
            public void onError(WeiboException arg0) {
                Log.d(TAG, "onError------" + arg0);

            }

            @Override
            public void onComplete4binary(ByteArrayOutputStream arg0) {
                Log.d(TAG, "onComplete4binary------" + arg0);

            }

            @Override
            public void onComplete(String js) {// 獲得Toak的就是js
                Log.d(TAG, "onComplete------" + js);
                //這裏json解析的做用下面會講到
                Json json = new Json();
                try {
                    json.pramas(js, Welcom.this);
                    handler.sendEmptyMessage(CODE_VIEWPAGER);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }



)};//這裏須要四個參數分別是String url, WeiboParameters params, String httpMethod, RequestListener listener
                            //第一個參數表明是新浪API裏的所給網站,第二,四個參數沒有和上面的方法同樣new出來,第三個參數新浪API一樣給出了是POST

}
View Code

如今已經得到Token了,那咱們要實現若是用戶已經受權過了以後,在token的有效期以前都不用從新受權的效果ide

這就須要將得到的Token解析而且使用SharedPreferences來存儲網站

 1 //新建一個類
 2 //Token的返回數據請查看新浪微博API
 3 public class Json{
 4 //須要獲取數據源,因此在方法上加了一個String js,至於爲何還要加一個context在Sharde會講到
 5 public void json(String js,Context context){
 6 JsonObject json=new JsonObject(js);
 7 //這裏咱們只需三個數據
 8 String access_token = jsonObject.getString("access_token");
 9 long expires_in = jsonObject.getLong("expires_in");//有效期
10 String uid = jsonObject.getString("uid");//用戶id
11 Sharder sharder = new Sharder();
12 //將解析獲得的數據存入SharedPreferences
13 sharder.putToke(context, access_token, expires_in);
14 sharder.putuid(context, uid);
15 }
16 
17 }
Json.class
 1 //新建一個類來存儲數據
 2 public class Sharde(){
 3 
 4 public void putToken(Context context,String token,long expires_in){
 5 //要使用context調用get來獲取SharedPreferences,而json裏要使用此方法,因此json裏面也要有context
 6 SharedPreferences preferences=context.getSharedPreferences("token",SharedPreferences.Moden_PRI);//第一個是SharedPreferences的名字,第二個是數據存儲形式
 7 Editor editor= preferences.edit();
 8 editor.putString("token",token);//由於這要放token的值因此要在方法裏寫上
 9 editor.putLong("expires_in",expires_in);
10 //這個不要忘了
11 editor.commt;
12 }
13 public void putuid(Context context,String uid){
14 SharedPreferences preferences=context.getSharedPreferences("uid",SharedPreferences.Moden_PRI);
15 Editor editor= preferences.edit();
16 editor.putString("token",token);
17 //這個不要忘了
18 editor.commt;
19 }
20 //將SharedPreferences,的數據拿出來,以供判斷使用
21 public Oauth2AccessToken getToken(){
22 SharedPreferences preferences=context.getSharedPreferences("token",SharedPreferences.Moden_PRI);
23 String token = preferences.getString("token", null);//第二個參數是默認值的意思
24 long expires_in = preferences.getLong("expires_in", 0);
25 return  new    Oauth2AccessToken(token,expires_in+"") ;//返回Token,裏面不能放long型因此要轉成String型
26 }
27 
28 public String getuid(){
29 SharedPreferences preferences = context.getSharedPreferences("uid",
30                 context.MODE_PRIVATE);
31         String uid = preferences.getString("uid", null);
32         return uid;
33 }
34 
35 
36 }
Sharde.class

如今咱們要在主Activty裏面判斷ui

 1 public void onClick(View v) {
 2     if (sharder.getToke(Welcom.this).isSessionValid()) {
 3         Intent intent=new Intent(Welcom.this,Viewpager.class);
 4         startActivity(intent);//這是實現跳轉的,下篇文章再說
 5         return;
 6     }
 7     getcode();
 8 
 9 }
10 });

最後不要忘記註冊Activty和權限this

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 

 這並非一篇教程,僅僅是我給老師教的做業,代碼也不是在eclipse裏面打的,裏面可能有不少錯誤,但願你們多多見諒,源代碼明天再上傳,今天太累了url

相關文章
相關標籤/搜索