android客户端登录代码,开源中国android客户端学习:配置免登录实现

首先实在Main activity中的onCreate()方法

调用读取登录信息的方法

appContext.initLoginInfo();

这个appContext是一个Context实例appContext = (AppContext)

getApplication();

所以这里不要纠结。

我们打开initLoginInfo()方法看看

public void initLoginInfo() {

User loginUser = getLoginInfo();

if(loginUser!=null &&

loginUser.getUid()>0

&& loginUser.isRememberMe()){

this.loginUid = loginUser.getUid();

this.login = true;

}else{

this.Logout();

}

}

其中这个User是用户信息的bean类

这里做的如下判断,用户信息不为空,用户的Uid大于零,用户配置了记住登录

我们看下getLoginInfo()里面做了什么

public User getLoginInfo() {

User lu = new User();

lu.setUid(StringUtils.toInt(getProperty("user.uid"), 0));

lu.setName(getProperty("user.name"));

lu.setFace(getProperty("user.face"));

lu.setAccount(getProperty("user.account"));

lu.setPwd(CyptoUtils.decode("oschinaApp",getProperty("user.pwd")));

lu.setLocation(getProperty("user.location"));

lu.setFollowers(StringUtils.toInt(getProperty("user.followers"),

0));

lu.setFans(StringUtils.toInt(getProperty("user.fans"), 0));

lu.setScore(StringUtils.toInt(getProperty("user.score"),

0));

lu.setRememberMe(StringUtils.toBool(getProperty("user.isRememberMe")));

return lu;

}

StringUtils.toXX()系列方法就是进行数据类型转换,我们这里忽略它的影响,看来拿到登录信息就是靠这个方法的getProperty(String

key)这个方法

那我们导航进去看看

这个方法是AppContext. getProperty(String key)

这个AppContext是继承Application类的全局类。

这是getProperty(String key的实现

public String getProperty(String key){

return AppConfig.getAppConfig(this).get(key);

}

这里只是利用上下文变量对AppConfig进行初始化

public static AppConfig getAppConfig(Context context) {

if (appConfig == null) {

appConfig = new AppConfig();

appConfig.mContext = context;

}

return appConfig;

}

接着调用AppConfig的get(String key)方法

下面打开 用AppConfig的get(String key)方法

public String get(String key) {

Properties props = get();

return (props != null) ? props.getProperty(key) : null;

}

这个是调用的get()方法,我们根据注释了解一下

public Properties get() {

FileInputStream fis = null;

// 一种继承自HashTable的数据结构

Properties props = new Properties();

try {

// 读取files目录下的config

// fis = activity.openFileInput(APP_CONFIG);

// 读取app_config目录下的config

File dirConf = mContext.getDir(APP_CONFIG,

Context.MODE_PRIVATE);

fis = new FileInputStream(dirConf.getPath() + File.separator

+ APP_CONFIG);

props.load(fis);

} catch (Exception e) {

} finally {

try {

fis.close();

} catch (Exception e) {

}

}

return props;

}

关键就在于这个类了Properties,这是java.lang.Properties中的一个类常用来做配置文件,上述操作读取的是/data/data/应用包名/app_config

目录下的config文件,如下图是在File Explorer打开的目录。

a4c26d1e5885305701be709a3d33442f.png

之前我学到的做法是将用户信息保存在sharedPreferences中,然后每次打开应用就读取登录信息,然而开源中国客户端的做法,却是很是特别。

可以用一些软件打开这个config文件(我用浏览器打开的),可以看到这一系列的能过户信息

#Sun Jul 05 08:42:29 GMT 2015

cookie=oscid\=Ws5aACwl+tjuXgd72zzwyfbh8wFSTqiLRYeTiUsndtqzRShHS7j6OgJWtGq/XwP6j8bN2/Xud+FyLYF+t6YsMj3/VY1/LtnH4yFzcv+eK1h56tNAVvulFw==;

save_image_path=/storage/emulated/0/OSChina/

user.score=0

user.name=\u8c22\u6587\u5fe0

user.account=wumengpiaoluo@163.com

user.uid=2282838

user.followers=1

user.face=2282838_100.jpg?t\=1417487687000

user.location=\u5e7f\u4e1c \u5e7f\u5dde

user.isRememberMe=true

APP_UNIQUEID=70740b81-b7b1-449f-b667-8051eaa6b366

user.pwd=1C6E1E50080CB1D121D0B4EDCB4203FE

user.fans=0

所以实际上通过查看Properties的源码可以知道,其实它就是把配置信息按照键值对的方式读取出来。我们简单看下Properties中的源码,主要是这个load(Reader

reader)方法

public class Properties extends Hashtable{

……………

@SuppressWarnings("fallthrough")

public synchronized void load(Reader in) throws IOException

{

if (in == null) {

throw new NullPointerException();

}

int mode = NONE, unicode = 0, count = 0;

char nextChar, buf[] = new char[40];

int offset = 0, keyLength = -1, intVal;

boolean firstChar = true;

BufferedReader br = new BufferedReader(in);

while (true) {

intVal = br.read();

if (intVal == -1) {

break;

}

nextChar = (char) intVal;

if (offset == buf.length) {

char[] newBuf = new char[buf.length * 2];

System.arraycopy(buf, 0, newBuf, 0, offset);

buf = newBuf;

}

if (mode == UNICODE) {

int digit = Character.digit(nextChar, 16);

if (digit >= 0) {

unicode = (unicode << 4) +

digit;

if (++count < 4) {

continue;

}

} else if (count <= 4) {

throw new IllegalArgumentException("Invalid Unicode sequence:

illegal character");

}

mode = NONE;

buf[offset++] = (char) unicode;

if (nextChar != '\n') {

continue;

}

}

if (mode == SLASH) {

mode = NONE;

switch (nextChar) {

case '\r':

mode = CONTINUE; // Look for a following \n

continue;

case '\n':

mode = IGNORE; // Ignore whitespace on the next line

continue;

case 'b':

nextChar = '\b';

break;

case 'f':

nextChar = '\f';

break;

case 'n':

nextChar = '\n';

break;

case 'r':

nextChar = '\r';

break;

case 't':

nextChar = '\t';

break;

case 'u':

mode = UNICODE;

unicode = count = 0;

continue;

}

} else {

switch (nextChar) {

case '#':

case '!':

if (firstChar) {

while (true) {

intVal = br.read();

if (intVal == -1) {

break;

}

nextChar = (char) intVal;

if (nextChar == '\r'

nextChar == '\n') {

break;

}

}

continue;

}

break;

case '\n':

if (mode == CONTINUE) { // Part of a \r\n sequence

mode = IGNORE; // Ignore whitespace on the next line

continue;

}

// fall into the next case

case '\r':

mode = NONE;

firstChar = true;

if (offset > 0

(offset == 0 && keyLength == 0))

{

if (keyLength == -1) {

keyLength = offset;

}

String temp = new String(buf, 0, offset);

put(temp.substring(0, keyLength), temp

.substring(keyLength));

}

keyLength = -1;

offset = 0;

continue;

case '\\':

if (mode == KEY_DONE) {

keyLength = offset;

}

mode = SLASH;

continue;

case ':':

case '=':

if (keyLength == -1) { // if parsing the key

mode = NONE;

keyLength = offset;

continue;

}

break;

}

if (Character.isWhitespace(nextChar)) {

if (mode == CONTINUE) {

mode = IGNORE;

}

// if key length == 0 or value length == 0

if (offset == 0

offset == keyLength

mode == IGNORE) {

continue;

}

if (keyLength == -1) { // if parsing the key

mode = KEY_DONE;

continue;

}

}

if (mode == IGNORE

mode == CONTINUE) {

mode = NONE;

}

}

firstChar = false;

if (mode == KEY_DONE) {

keyLength = offset;

mode = NONE;

}

buf[offset++] = nextChar;

}

if (mode == UNICODE && count

<= 4) {

throw new IllegalArgumentException("Invalid Unicode sequence:

expected format \\uxxxx");

}

if (keyLength == -1 && offset

> 0) {

keyLength = offset;

}

if (keyLength >= 0) {

String temp = new String(buf, 0, offset);

String key = temp.substring(0, keyLength);

String value = temp.substring(keyLength);

if (mode == SLASH) {

value += "\u0000";

}

put(key, value);

}

}

……………

}

在这个方面里面主要是对编码方式进行判断,然后将属性按照键值对形式存储。

(具体还得研究一下这个Properties的具体实现原理,这里暂时忽略)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
Smack是一个开源的XMPP客户端库,可用于在Android平台上构建即时通信应用程序。在使用Smack的Android应用程序中,需要使用Smack-Android库来处理网络和连接管理。 以下是一个简单的示例代码,演示如何使用Smack-Android库连接到XMPP服务器并发送消息: 1. 添加依赖库 在项目的build.gradle文件中添加以下依赖: ``` dependencies { implementation 'org.igniterealtime.smack:smack-android-extensions:4.4.0' implementation 'org.igniterealtime.smack:smack-tcp:4.4.0' } ``` 2. 初始化连接 在应用程序启动时,需要初始化XMPPConnection对象,并且连接到XMPP服务器。 ``` XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() .setUsernameAndPassword("username", "password") .setXmppDomain("example.com") .setHost("xmpp.example.com") .setPort(5222) .setSecurityMode(ConnectionConfiguration.SecurityMode.required) .build(); XMPPTCPConnection connection = new XMPPTCPConnection(config); try { connection.connect(); connection.login(); // Connection successful } catch (SmackException | IOException | XMPPException e) { e.printStackTrace(); // Connection failed } ``` 3. 发送消息 连接成功后,可以使用XMPPConnection对象发送消息。 ``` ChatManager chatManager = ChatManager.getInstanceFor(connection); Chat chat = chatManager.createChat("[email protected]"); try { chat.sendMessage("Hello, World!"); } catch (SmackException.NotConnectedException | InterruptedException e) { e.printStackTrace(); } ``` 这是一个简单的Smack-Android示例,用于连接到XMPP服务器并发送消息。当然,在实际应用程序中可能需要更多的功能和处理,但这个示例提供了一个入门的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值