/*
* 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 org.json.JSONObject;
import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import com.renren.api.connect.android.bean.FeedParam;
import com.renren.api.connect.android.view.RenrenDialog;
import com.renren.api.connect.android.view.RenrenDialogListener;
import com.renren.api.connect.android.view.RenrenDialogListenerHelper;
/**
* 封装对人人的请求,如:显示登录界面、退出登录、请求REST APIs等。
*
* @author yong.li@opi-corp.com
*/
public class Renren {
public static final String REDIRECT_URI = "rrconnect://success";
public static final String CANCEL_URI = "rrconnect://cancel";
public static final String DISPLAY_VALUE = "android";
static final String RRCONNECT_NUMBER = "2";
static final String RESPONSE_FORMAT_JSON = "json";
static final String RESPONSE_FORMAT_XML = "xml";
static final String TOKEN = "auth_token";
static final String SESSION_KEY = "session_key";
static final String AUTO_LOGIN = "autoLogin";
static final String RENREN_CONFIG = "renren_connect_config";
static final String RENREN_SESSION_KEY = "renren_connect_session_key";
static final String RENREN_SESSION_KEY_CREATE_TIME = "renren_connect_session_key_create_time";
private String authToken = null;
private String sessionKey = null;
private String apiKey;
private String apiSecret;
private Context context;
public static final String LOGIN_URL = "http://login.api.renren.com/connect/touch_login.do";
static final String POST_FEED_URL = "http://www.connect.renren.com/feed/iphone/feedPrompt";
static final String RESTSERVER_URL = "http://api.renren.com/restserver.do";
private static final long ONE_HOUR = 1000 * 60 * 60;
public Renren(Context context, String apiKey, String apiSecret) {
if (context == null || apiKey == null || apiSecret == null) throw new RuntimeException(
"context, apiKey,apiSecret必须提供");
this.context = context;
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.restoreSessionKey();
if (this.sessionKey != null) Log.i(Util.LOG_TAG, "restore success sessionKey:"
+ this.sessionKey);
}
/**
* 获取登录authToken,并获取sessionkey。如果用户已经登录监听器将不会被调用。
*
* @param context
* @param listener
*/
public void authorize(final RenrenDialogListener listener) {
if (this.sessionKey != null) return;
// 调用CookieManager.getInstance之前
// 必须先调用CookieSyncManager.createInstance
CookieSyncManager.createInstance(context);
CookieManager.getInstance().setCookie("http://login.api.renren.com/connect/",
"mobile-connect=beta");
Bundle params = new Bundle();
params.putString("api_key", apiKey);
params.putString("rrconnect", RRCONNECT_NUMBER);
params.putString("display", DISPLAY_VALUE);
params.putString("next", REDIRECT_URI);
String url = LOGIN_URL + "?" + Util.encodeUrl(params);
if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
Util.showAlert(context, "没有权限", "应用需要访问互联网的权限");
} else {
RenrenDialogListenerHelper helper = new RenrenDialogListenerHelper();
helper.addDialogListener(new RenrenDialogListenerHelper.DefaultRenrenDialogListener() {// 耗时的流氓监听器
@Override
public void onComplete(Bundle values) {
CookieSyncManager.getInstance().sync();
authToken = values.getString(TOKEN);
if (authToken != null) {
Log.d(Util.LOG_TAG, "Success obtain auth_token=" + authToken);
boolean autoLogin = values.getBoolean(AUTO_LOGIN, false);
if (!autoLogin) {
if (values.getString("url").indexOf("autoLogintrue") > -1) autoLogin = true;
}
initSessionKey(autoLogin);
}
}
});
helper.addDialogListener(listener);
new RenrenDialog(context, url, helper).show();
}
}
private void initSessionKey(boolean autoLogin) {
Bundle params = new Bundle();
params.putString("method", "auth.getSession");
params.putString("auth_token", authToken);
String sk = this.request(params, RESPONSE_FORMAT_JSON);
try {
JSONObject obj = new JSONObject(sk);
this.sessionKey = obj.getString("session_key");
Log.i(Util.LOG_TAG, "---login success sessionKey:" + this.sessionKey);
if (autoLogin) this.storeSessionKey(this.sessionKey);
else this.storeSessionKey(null);
} catch (Throwable e) {
throw new RuntimeException(e.getMessage(), e);
}
}
private void storeSessionKey(String sessionKey) {
Editor editor = context.getSharedPreferences(RENREN_CONFIG, Context.MODE_PRIVATE).edit();
if (sessionKey != null) {
editor.putString(RENREN_SESSION_KEY, sessionKey);
editor.putLong(RENREN_SESSION_KEY_CREATE_TIME, System.currentTimeMillis());
} else {
this.clearPersistSession();
}
editor.commit();
}
private void restoreSessionKey() {
SharedPreferences sp = context.getSharedPreferences(RENREN_CONFIG, Context.MODE_PRIVATE);
String sk = sp.getString(RENREN_SESSION_KEY, null);
if (sk == null) return;
long createTime = sp.getLong(RENREN_SESSION_KEY_CREATE_TIME, 0);
long life = Long.parseLong(sk.split("\\.")[2]) * 1000;
long currenct = System.currentTimeMillis();
if ((createTime + life) < (currenct - ONE_HOUR)) this.clearPersistSession();
else this.sessionKey = sk;
}
private void clearPersistSession() {
Editor editor = context.getSharedPreferences(RENREN_CONFIG, Context.MODE_PRIVATE).edit();
editor.remove(RENREN_SESSION_KEY);
editor.remove(RENREN_SESSION_KEY_CREATE_TIME);
editor.commit();
}
/**
* 退出登录
*
* @param context
* @return
*/
public String logout(Context context) {
Util.clearCookies(context);
this.clearPersistSession();
authToken = null;
sessionKey = null;
return "true";
}
/**
* 发新鲜事
*
* @param feedParam
* @param listener
*/
public void feed(FeedParam feedParam, final RenrenDialogListener listener) {
Bundle params = new Bundle();
params.putString("api_key", apiKey);
params.putString("session_key", sessionKey);
params.putString("preview", "true");
没有合适的资源?快使用搜索试试~ 我知道了~
renrenwang.rar_renrenwang_人人网
共97个文件
class:46个
java:22个
png:13个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 25 浏览量
2022-09-24
03:32:08
上传
评论
收藏 239KB RAR 举报
温馨提示
人人网,自己下载看了就知道了,不是自己写的.只是资源共享
资源推荐
资源详情
资源评论
收起资源包目录
renrenwang.rar (97个子文件)
人人备份
Example
bin
classes.dex 47KB
Example.apk 59KB
com
renren
api
connect
android
Util.class 9KB
example
ImageAdapter$ViewHolder.class 559B
PostFeedListener.class 2KB
SimpleRequestListener$1.class 982B
TestConnectButtonListener$1.class 2KB
R$layout.class 484B
FriendRequestListener.class 5KB
FriendRequestListener$2.class 2KB
FriendRequestListener$1.class 1KB
R$id.class 849B
R$drawable.class 1KB
TestConnectButtonListener$2.class 1KB
Example.class 7KB
R$attr.class 400B
R.class 671B
ImageAdapter.class 4KB
SimpleRequestListener.class 3KB
R$string.class 458B
TestConnectButtonListener$3.class 1KB
xml
RestHandler.class 405B
FriendHandler.class 2KB
RestSaxParser.class 2KB
TestConnectButtonListener$4.class 1KB
TestConnectButtonListener.class 2KB
exception
RenrenError.class 1KB
AsyncRenren$2.class 2KB
bean
FeedParam.class 2KB
Renren.class 9KB
RequestListenerHelper.class 2KB
view
RenrenDialogListenerHelper$DefaultRenrenDialogListener.class 2KB
RenrenDialog.class 6KB
ConnectButton$LoginDialogListener.class 2KB
RenrenDialog$RenrenWebViewClient.class 4KB
ConnectButtonListenerHelper$DefaultConnectButtonListener.class 1KB
ConnectButton$1.class 764B
ConnectButtonListenerHelper.class 2KB
ConnectButton.class 4KB
ConnectButton$LogoutRequestListener.class 2KB
ConnectButtonListener.class 358B
RenrenDialogListener.class 371B
RenrenDialogListenerHelper.class 3KB
RequestListenerHelper$DefaultRequestListener.class 2KB
AsyncRenren.class 2KB
AsyncRenren$1.class 2KB
RequestListener.class 316B
Renren$1.class 2KB
resources.ap_ 35KB
res
drawable-ldpi
icon.png 2KB
.svn
drawable-hdpi
icon.png 4KB
.svn
drawable
renren_login_button_up.png 5KB
renren_logout_button_down.png 5KB
renren_login_button_down.png 5KB
renren_login_button.xml 292B
logout.png 2KB
renren_logout_button.xml 300B
.picasa.ini 36B
logout_down.png 2KB
renren_android_title_logo.png 3KB
renren_connect.png 5KB
login_button.xml 261B
login.png 2KB
renren_logout_button_up.png 5KB
.svn
logout_button.xml 269B
login_down.png 2KB
values
strings.xml 132B
drawable-mdpi
icon.png 3KB
layout
main.xml 4KB
friend.xml 662B
assets
default.properties 504B
gen
com
renren
api
connect
android
example
R.java 2KB
.settings
org.eclipse.core.resources.prefs 161B
src
com
renren
api
connect
android
example
SimpleRequestListener.java 2KB
Example.java 6KB
FriendRequestListener.java 6KB
TestConnectButtonListener.java 2KB
PostFeedListener.java 1KB
xml
FriendHandler.java 2KB
RestSaxParser.java 919B
RestHandler.java 240B
.svn
exception
RenrenError.java 2KB
Renren.java 10KB
AsyncRenren.java 3KB
bean
FeedParam.java 2KB
view
RenrenDialogListener.java 818B
ConnectButtonListenerHelper.java 2KB
RenrenDialog.java 6KB
RenrenDialogListenerHelper.java 3KB
ConnectButton.java 5KB
ConnectButtonListener.java 813B
Util.java 7KB
RequestListener.java 1KB
RequestListenerHelper.java 2KB
.project 843B
.classpath 280B
AndroidManifest.xml 837B
共 97 条
- 1
资源评论
寒泊
- 粉丝: 78
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功