/*
* 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");
没有合适的资源?快使用搜索试试~ 我知道了~
android 校内网客户端源代码
共103个文件
class:52个
java:23个
png:13个
3星 · 超过75%的资源 需积分: 9 165 下载量 159 浏览量
2011-01-24
10:38:38
上传
评论 3
收藏 252KB ZIP 举报
温馨提示
android 校内网客户端源代码 帮助学习如何用android写客户端
资源推荐
资源详情
资源评论
收起资源包目录
android 校内网客户端源代码 (103个子文件)
resources.ap_ 35KB
Example.apk 60KB
Renren.class 9KB
Util.class 9KB
Example.class 6KB
RenrenDialog.class 6KB
FriendRequestListener.class 5KB
RenrenDialog$RenrenWebViewClient.class 4KB
ConnectButton.class 4KB
ImageAdapter.class 4KB
SimpleRequestListener.class 3KB
RenrenDialogListenerHelper.class 3KB
ConnectButtonListenerHelper.class 2KB
ConnectButton$LoginDialogListener.class 2KB
FriendHandler.class 2KB
TestConnectButtonListener.class 2KB
RequestListenerHelper.class 2KB
PostFeedListener.class 2KB
RenrenDialogListenerHelper$DefaultRenrenDialogListener.class 2KB
FeedParam.class 2KB
AsyncRenren$2.class 2KB
Renren$1.class 2KB
AsyncRenren$1.class 2KB
ConnectButton$LogoutRequestListener.class 2KB
AsyncRenren.class 2KB
RestSaxParser.class 2KB
FriendRequestListener$2.class 2KB
TestConnectButtonListener$1.class 2KB
RequestListenerHelper$DefaultRequestListener.class 2KB
ConnectButtonListenerHelper$DefaultConnectButtonListener.class 1KB
FriendRequestListener$1.class 1KB
TestConnectButtonListener$3.class 1KB
TestConnectButtonListener$2.class 1KB
TestConnectButtonListener$4.class 1KB
RenrenError.class 1KB
R$drawable.class 1KB
R$drawable.class 1KB
SimpleRequestListener$1.class 982B
R$id.class 821B
R$id.class 797B
ConnectButton$1.class 764B
R.class 671B
R.class 615B
ImageAdapter$ViewHolder.class 559B
R$layout.class 484B
R$layout.class 460B
R$string.class 458B
R$string.class 434B
RestHandler.class 405B
R$attr.class 400B
R$attr.class 376B
RenrenDialogListener.class 371B
ConnectButtonListener.class 358B
RequestListener.class 316B
.classpath 341B
classes.dex 48KB
Renren.java 10KB
Util.java 7KB
RenrenDialog.java 6KB
FriendRequestListener.java 6KB
Example.java 5KB
ConnectButton.java 5KB
AsyncRenren.java 3KB
RenrenDialogListenerHelper.java 3KB
R.java 2KB
R.java 2KB
ConnectButtonListenerHelper.java 2KB
RequestListenerHelper.java 2KB
FeedParam.java 2KB
FriendHandler.java 2KB
SimpleRequestListener.java 2KB
TestConnectButtonListener.java 2KB
RenrenError.java 2KB
PostFeedListener.java 1KB
RequestListener.java 1KB
RestSaxParser.java 919B
RenrenDialogListener.java 818B
ConnectButtonListener.java 813B
RestHandler.java 240B
renren_connect.png 5KB
renren_login_button_down.png 5KB
renren_login_button_up.png 5KB
renren_logout_button_up.png 5KB
renren_logout_button_down.png 5KB
icon.png 4KB
renren_android_title_logo.png 3KB
icon.png 3KB
login.png 2KB
login_down.png 2KB
icon.png 2KB
logout.png 2KB
logout_down.png 2KB
org.eclipse.core.resources.prefs 161B
.project 1KB
default.properties 504B
main.xml 3KB
AndroidManifest.xml 838B
friend.xml 662B
renren_logout_button.xml 300B
renren_login_button.xml 292B
共 103 条
- 1
- 2
chenjingsuzhou
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页