【已解决】 做搜索界面的时候出的bug,代码按照视频上写的,想知道这问题怎么解决,喜欢翻译Log的靠边
package com.example.taobaounion.utils;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.taobaounion.base.BaseApplication;
import com.example.taobaounion.model.domain.CacheWithDuration;
import com.example.taobaounion.model.domain.Histories;
import com.google.gson.Gson;
public class JsonCacheUtil {
public final static String JSON_CACHE_SP_NAME = "json_cache_sp_name";
private final SharedPreferences mSharedPreferences;
private final Gson mGson;
private static JsonCacheUtil sJsonCacheUtil = null;
public static JsonCacheUtil getInstance(){
if (sJsonCacheUtil == null) {
sJsonCacheUtil = new JsonCacheUtil();
}
return sJsonCacheUtil;
}
private JsonCacheUtil(){
mSharedPreferences = BaseApplication.getAppContext()
.getSharedPreferences(JSON_CACHE_SP_NAME, Context.MODE_PRIVATE);
mGson = new Gson();
}
public void saveCache(String key,Object value){
this.saveCache(key, value,-1L);
}
/**
* 增加一个闪存内容
* @param key
* @param value
* @param duration
*/
public void saveCache(String key,Object value,long duration){
SharedPreferences.Editor edit = mSharedPreferences.edit();
//保存一个有数据有时间的内容
String valueStr = mGson.toJson(value);
if (duration != -1L){
duration += System.currentTimeMillis();
}
CacheWithDuration cacheWithDuration = new CacheWithDuration(duration, valueStr);
String cacheWithTime = mGson.toJson(cacheWithDuration);
edit.putString(key,cacheWithTime);
edit.apply();
}
/**
* 删除一个闪存内容
* @param key
*/
public void delCache(String key){
mSharedPreferences.edit().remove(key).apply();
}
public <T> T getValue(String key, Class<T> clazz){
String valueWithDuration = mSharedPreferences.getString(key, null);
LogUtils.d(this,"valueWithDuration"+valueWithDuration);
if (valueWithDuration == null) {
return null;
}
CacheWithDuration cacheWithDuration = mGson.fromJson(valueWithDuration, CacheWithDuration.class);
//对时间进行判断
long duration = cacheWithDuration.getDuration();
//判断是否过了期
if (duration != -1 && duration-System.currentTimeMillis()<=0){
//没过期
return null;
}else{
//过期了
LogUtils.d(this,"clazz--->"+clazz);
LogUtils.d(this,"cacheWithDuration.getCache()-->"+cacheWithDuration.getCache());
return mGson.fromJson(cacheWithDuration.getCache(),clazz);
}
}
}

问题已解决,无须再回答
expected GEGIN_OBJECT,看好了,以后看到这个错误提示,都是json转对象的时候,json的语法(格式)错误。
这个时候就检查你的json就对了。
Json解析的错误,Json不要以[开头,要以{开头