Hawk 基本使用

本文介绍了Hawk,一款便捷的数据库存储框架。它基于SharedPreferences,由Android开发大神开源维护,使用AES加密,可选择SharedPreferences或SQLite,用Gson解析。文中还介绍了其用法、源码分析,以及用adb命令查看sharedpreferences,同时提醒APP升级改包名可能导致反序列化失败。

Hawk 是一个非常便捷的数据库  . 操作数据库只需一行代码 , 能存任何数据类型 .

github 地址: https://github.com/orhanobut/hawk

一、概念

SharedPreferences的使用大家应该非常熟悉啦。这是一种轻量级的存储简单配置信息的存储机制,以key-value的形式保存数据。

这里介绍一款基于SharedPreferences的的存储框架,是由Android开发大神Orhan Obut开源维护的,

它使用: 

AES 加密

能选择使用SharedPreferences  或者  SQLite

       Gson解析     (文章下方的Simple中有替换fastJson的版本)

提供:

安全数据持久化

能存储任何类型

 

二、用法

添加依赖

 

compile "com.orhanobut:hawk:2.0.1"

 

初始化

Hawk.init(context).build();

 

 

//基本数据类型
 Hawk.put("position","zz");

  Hawk.get("position");

二、用法源码分析

put:

HawkUtils.checkNull("Key", key);
log("Hawk.put -> key: " + key + ", value: " + value);

// If the value is null, delete it
if (value == null) {
  log("Hawk.put -> Value is null. Any existing value will be deleted with the given key");
  return delete(key);
}

// 1. Convert to text
String plainText = converter.toString(value);
log("Hawk.put -> Converted to " + plainText);
if (plainText == null) {
  log("Hawk.put -> Converter failed");
  return false;
}

// 2. Encrypt the text
String cipherText = null;
try {
  cipherText = encryption.encrypt(key, plainText);
  log("Hawk.put -> Encrypted to  " + cipherText);
} catch (Exception e) {
  e.printStackTrace();
}
if (cipherText == null) {
  log("Hawk.put -> Encryption failed");
  return false;
}

// 3. Serialize the given object along with the cipher text
String serializedText = serializer.serialize(cipherText, value);
log("Hawk.put -> Serialized to" + serializedText);
if (serializedText == null) {
  log("Hawk.put -> Serialization failed");
  return false;
}

// 4. Save to the storage
if (storage.put(key, serializedText)) {
  log("Hawk.put -> Stored successfully");
  return true;
} else {
  log("Hawk.put -> Store operation failed");
  return false;
}

 

get:

log("Hawk.get -> key: " + key);
if (key == null) {
  log("Hawk.get -> null key, returning null value ");
  return null;
}

// 1. Get serialized text from the storage
String serializedText = storage.get(key);
log("Hawk.get -> Fetched from storage : " + serializedText);
if (serializedText == null) {
  log("Hawk.get -> Fetching from storage failed");
  return null;
}

// 2. Deserialize
DataInfo dataInfo = serializer.deserialize(serializedText);
log("Hawk.get -> Deserialized");
if (dataInfo == null) {
  log("Hawk.get -> Deserialization failed");
  return null;
}

// 3. Decrypt
String plainText = null;
try {
  plainText = encryption.decrypt(key, dataInfo.cipherText);
  log("Hawk.get -> Decrypted to : " + plainText);
} catch (Exception e) {
  log("Hawk.get -> Decrypt failed: " + e.getMessage());
}
if (plainText == null) {
  log("Hawk.get -> Decrypt failed");
  return null;
}

// 4. Convert the text to original data along with original type
T result = null;
try {
  result = converter.fromString(plainText, dataInfo);
  log("Hawk.get -> Converted to : " + result);
} catch (Exception e) {
  log("Hawk.get -> Converter failed");
}

 

adb命令查看sharedpreferences

adb shell run-as com.example.android //对应包名

cd shared_prefs

注意点:APP升级过程中 修改包名称导致反序列化失败,get数据为null。

参考:https://www.cnblogs.com/zhangqie/p/10512215.html

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值