/*
* Copyright 2010 Renren, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.renren.api.connect.android;
import java.util.Set;
import java.util.TreeSet;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieSyncManager;
import com.renren.api.connect.android.bean.FeedParam;
import com.renren.api.connect.android.exception.RenrenError;
import com.renren.api.connect.android.view.RenrenAuthListener;
import com.renren.api.connect.android.view.RenrenDialog;
import com.renren.api.connect.android.view.RenrenDialogListener;
import com.renren.api.connect.android.view.RenrenFeedListener;
import com.renren.api.connect.android.view.RenrenWidgetListener;
/**
* 封装对人人的请求,如:显示登录界面、退出登录、请求 人人 APIs等。
*
* @author yong.li@opi-corp.com
*/
public class Renren {
/**
* 如果服务器redirect该地址,SDK会认为用户做了取消操作。
*/
public static final String CANCEL_URI = "rrconnect://cancel";
/**
* 如果服务器redirect该地址,SDK会认为用户做了确认操作。
*/
public static final String SUCCESS_URI = "rrconnect://success";
/**
* 人人登录和授权的地址
*/
public static final String AUTHORIZE_URL = "https://graph.renren.com/oauth/authorize";
/**
* 接口支持的数据格式
*/
public static final String RESPONSE_FORMAT_JSON = "json";
/**
* 接口支持的数据格式
*/
public static final String RESPONSE_FORMAT_XML = "xml";
private static final String DEFAULT_REDIRECT_URI = "http://graph.renren.com/oauth/login_success.html";
public static final String WIDGET_CALLBACK_URI = "http://widget.renren.com/callback.html";
private static final String POST_FEED_URL = "http://www.connect.renren.com/feed/iphone/feedPrompt";
private static final String RESTSERVER_URL = "http://api.renren.com/restserver.do";
private String sessionKey = null;
private String sessionSecret = null;
private String apiKey;
private AccessTokenManager accessTokenManager;
/**
* 构造Renren对象,开发者可以使用该类调用人人网提供的接口。
*
* @param apiKey
*/
public Renren(String apiKey) {
if (apiKey == null) {
throw new RuntimeException("apiKey必须提供");
}
this.apiKey = apiKey;
}
/**
* 用户授权后更新accessToken和sessionKey。
*
* @param accessToken
* @throws RenrenError 换取sessionKey出现错误。
* @throws RuntimeException 出现其他错误。
*/
void updateAccessToken(String accessToken) throws RenrenError, RuntimeException {
this.accessTokenManager.updateAccessToken(accessToken);
this.updateSessionKey();
}
private void updateSessionKey() {
this.sessionKey = this.accessTokenManager.getSessionKey();
this.sessionSecret = this.accessTokenManager.getSessionSecret();
if (this.sessionKey == null || this.sessionSecret == null) {
Log.i(Util.LOG_TAG, "sessionKey sessionSecret is null!");
}
}
/**
* 尝试读取sessionKey;如果用户在一天内登录过并且没有退出返回true。
*
* @param context
* @return
*/
public boolean restorSessionKey(Context context) {
this.accessTokenManager = new AccessTokenManager(context);
this.accessTokenManager.restoreSessionKey();
this.updateSessionKey();
if (this.sessionKey != null && this.sessionSecret != null) {
return true;
}
return false;
}
/**
* 用户登录和授权(Web Server Flow)。 用该方法完成的授权不能调用RenRes.request方法;对人人
* API的调用需要通过开发者自己的服务器来代理。
*
* @param activity
* @param listener
* @param redirectUrl 用来处理登录和授权的开发者的服务器地址。
*/
public void authorize(Activity activity, final RenrenAuthListener listener, String redirectUrl) {
this.authorize(activity, null, listener, redirectUrl);
}
/**
* 用户登录和授权(Web Server Flow)。 用该方法完成的授权不能调用RenRes.request方法;对人人
* API的调用需要通过开发者自己的服务器来代理。
*
* @param activity
* @param permissions 应用想拥有的权限列表。
* @param listener
* @param redirectUrl 用来处理登录和授权的开发者的服务器地址。
*/
public void authorize(Activity activity, String[] permissions,
final RenrenAuthListener listener, String redirectUrl) {
if (this.isSessionKeyValid()) {
listener.onComplete(new Bundle());
return;
}
RenrenDialogListener rdl = RenrenListenerFactory.genWebServerFlowRenrenDialogListener(this,
listener, redirectUrl);
this.authorize(activity, permissions, rdl, redirectUrl, "code");
}
/**
* 完成登录并获取sessionkey(User-Agent Flow)。
*
* @param activity
* @param listener
*/
public void authorize(Activity activity, final RenrenAuthListener listener) {
this.authorize(activity, null, listener);
}
/**
* 完成登录并获取sessionkey(User-Agent Flow)。
*
* @param activity
* @param permissions 应用想拥有的权限列表。
* @param listener
*/
public void authorize(Activity activity, String[] permissions, final RenrenAuthListener listener) {
if (this.isSessionKeyValid()) {
listener.onComplete(new Bundle());
return;
}
RenrenDialogListener rdl = RenrenListenerFactory.genUserAgentFlowRenrenDialogListener(this,
listener, DEFAULT_REDIRECT_URI);
this.authorize(activity, permissions, rdl, DEFAULT_REDIRECT_URI, "token");
}
private void authorize(Activity activity, String[] permissions,
final RenrenDialogListener listener, String redirectUrl, String responseType) {
// 调用CookieManager.getInstance之前
// 必须先调用CookieSyncManager.createInstance
CookieSyncManager.createInstance(activity);
Bundle params = new Bundle();
params.putString("client_id", apiKey);
params.putString("redirect_uri", redirectUrl);
params.putString("response_type", responseType);
params.putString("display", "touch");
if (permissions != null && permissions.length > 0) {
String scope = TextUtils.join(" ", permissions);
params.putString("scope", scope);
}
String url = AUTHORIZE_URL + "?" + Util.encodeUrl(params);
if (activity.checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
Util.showAlert(activity, "没有权限", "应用需要访问互联网的权限");
} else {
new RenrenDialog(activity, url, listener).show();
}
}
/**
* 退出登录
*
* @param context
* @return
*/
public String logout(Context context) {
Util.clearCookies(context);
this.accessTokenManager.clearPersistSession();
没有合适的资源?快使用搜索试试~ 我知道了~
Renren-api-android
共66个文件
java:32个
png:18个
xml:9个
4星 · 超过85%的资源 需积分: 10 7 下载量 18 浏览量
2011-12-09
13:05:57
上传
评论
收藏 143KB ZIP 举报
温馨提示
Renren-api-android 学习Renren开发的可能用得到
资源推荐
资源详情
资源评论
收起资源包目录
Renren-api-android-sdk.zip (66个子文件)
renren_platform-renren-api-android-sdk-4052bb1aec12
example
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable
logout.png 2KB
logout_down.png 2KB
login_button.xml 261B
login.png 2KB
logout_button.xml 269B
login_down.png 2KB
values
strings.xml 132B
drawable-mdpi
icon.png 3KB
layout
main.xml 5KB
friend.xml 661B
assets
renren.png 25KB
default.properties 420B
gen
com
renren
api
connect
android
example
R.java 3KB
R.java 3KB
src
com
renren
api
connect
android
example
SimpleRequestListener.java 2KB
WidgetListener.java 2KB
Example.java 9KB
FriendRequestListener.java 6KB
SimpleRenrenAuthListener.java 1KB
TestConnectButtonListener.java 2KB
PostFeedListener.java 1KB
ExampleUtil.java 905B
xml
FriendHandler.java 2KB
RestSaxParser.java 919B
RestHandler.java 240B
.project 1KB
.classpath 345B
AndroidManifest.xml 838B
renren_android_connect
res
drawable-ldpi
renren_connect_icon.png 5KB
drawable-hdpi
renren_connect_icon.png 5KB
drawable
renren_login_button_up.png 5KB
renren_android_title_bg.png 272B
renren_logout_button_down.png 5KB
renren_login_button_down.png 5KB
renren_login_button.xml 292B
renren_logout_button.xml 300B
renren_android_title_logo.png 918B
renren_connect.png 5KB
renren_logout_button_up.png 5KB
drawable-mdpi
renren_connect_icon.png 7KB
default.properties 385B
gen
com
renren
api
connect
android
R.java 1KB
src
com
renren
api
connect
android
RenrenListenerFactory.java 11KB
exception
RenrenAuthError.java 2KB
RenrenError.java 2KB
Renren.java 15KB
AsyncRenren.java 5KB
bean
FeedParam.java 2KB
view
RenrenDialogListener.java 1KB
ConnectButtonListenerHelper.java 3KB
RenrenDialog.java 6KB
RenrenWidgetListener.java 532B
RenrenFeedListener.java 794B
RenrenAuthListener.java 924B
ConnectButton.java 4KB
ConnectButtonListener.java 1KB
Util.java 13KB
RequestListener.java 1KB
AccessTokenManager.java 5KB
RequestListenerHelper.java 2KB
.project 858B
.classpath 280B
AndroidManifest.xml 180B
doc
Renren_android_connect_readme.doc 32KB
共 66 条
- 1
资源评论
- dykun_12252013-01-25就当时做项目的时候看了下,因为刚Android所以下的,
dafei8851521
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功