Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
microshow committed May 15, 2021
1 parent 7291ede commit ae3c41c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import io.microshow.retrofitgo.RetrofitClient;

Expand Down Expand Up @@ -39,29 +41,29 @@ public static <T> void saveCacheData(String cacheKey, T model) {
}
}

public static <T> T getCacheData(String cacheKey, Type typeOfT) {
try {
if (!TextUtils.isEmpty(cacheKey)) {
String data = getSharedPreferences().getString(cacheKey, null);
Log.e("CacheSpUtils","getCacheData cacheKey="+cacheKey+";json="+data);
return !TextUtils.isEmpty(data) ? new Gson().fromJson(data, typeOfT) : null;
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
// public static <T> T getCacheData(String cacheKey, Type typeOfT) {
// try {
// if (!TextUtils.isEmpty(cacheKey)) {
// String data = getSharedPreferences().getString(cacheKey, null);
// Log.e("CacheSpUtils","getCacheData cacheKey="+cacheKey+";json="+data);
// return !TextUtils.isEmpty(data) ? new Gson().fromJson(data, typeOfT) : null;
// } else {
// return null;
// }
// } catch (Exception e) {
// return null;
// }
// }

public static <T> T getCacheData(String cacheKey, Type typeOfT, Type typeOfList) {
public static <T> T getCacheData(String cacheKey, Type typeOfT) {
try {
if (!TextUtils.isEmpty(cacheKey)) {
String data = getSharedPreferences().getString(cacheKey, null);
Log.e("CacheSpUtils","getCacheData cacheKey="+cacheKey+";json="+data);
if (data != null && !TextUtils.isEmpty(data)) {
if (data.startsWith("[") && data.endsWith("]")) {//json数组
Log.e("CacheSpUtils","getCacheData json array");
return new Gson().fromJson(data, typeOfList);
return new Gson().fromJson(data, new TypeToken<T>(){}.getType());
} else {
Log.e("CacheSpUtils","getCacheData json obj");
return new Gson().fromJson(data, typeOfT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import android.annotation.SuppressLint;
import android.support.annotation.NonNull;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import io.microshow.retrofitgo.arch.Resource;
import io.microshow.retrofitgo.cache.CachePolicyMode;
import io.microshow.retrofitgo.cache.CacheSpUtils;
Expand Down Expand Up @@ -83,7 +78,7 @@ private void saveResult(T item) {
}

private <T> T getCacheResult(String cacheKey) {
return CacheSpUtils.getCacheData(cacheKey, ClassTypeReflect.getModelClazz(getClass()), new TypeToken<List<Object>>(){}.getType());
return CacheSpUtils.getCacheData(cacheKey, ClassTypeReflect.getModelClazz(getClass()));
}

//获取缓存的key
Expand Down

0 comments on commit ae3c41c

Please sign in to comment.