报错:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
代码:
JsonCacheUtil:
public class JsonCacheUtil {
    public static final String JSON_CACHE_SP_NAME = "json_cache_sp_name";
    private final SharedPreferences mSharedPreferences;
    private final Gson mGson;
    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);
    }
    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();
    }
    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);
        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 {
                //没有过期
            String cache = cacheWithDuration.getCache();
            T result = mGson.fromJson(cache,clazz);
            return result;
        }
    }
    private static JsonCacheUtil sJsonCacheUtil = null;
    public static JsonCacheUtil getInstance(){
        if (sJsonCacheUtil == null) {
            sJsonCacheUtil = new JsonCacheUtil();
        }
        return sJsonCacheUtil;
    }
}
SearchPresenter:
public class SearchPresenter implements ISearchPresenter {
    private final Api mApi;
    private final JsonCacheUtil mJsonCacheUtil;
    private ISearchPageCallback mSearchViewCallback = null;
    private String  mCurrentKeyword = null;
    public SearchPresenter() {
        RetrofitManager instance = RetrofitManager.getInstance();
        Retrofit retrofit = instance.getRetrofit();
        mApi = retrofit.create(Api.class);
        mJsonCacheUtil = JsonCacheUtil.getInstance();
    }
    public static final int DEFAULT_PAGE = 0;
    /**
     * 搜索的当前页面
     */
    private int mCurrentPage = DEFAULT_PAGE;
    @Override
    public void getHistories() {
        Histories histories = mJsonCacheUtil.getValue(KEY_HISTORIES, Histories.class);
        if (mSearchViewCallback != null &&
                histories != null &&
                histories.getHistories() != null &&
                histories.getHistories().size() != 0) {
            mSearchViewCallback.onHistoriesLoaded(histories.getHistories());
        }
    }
    @Override
    public void delHistories() {
        mJsonCacheUtil.delCache(KEY_HISTORIES);
    }
    public static final String KEY_HISTORIES = "key_histories";
    public static final int DEFAULT_HISTORIES_SIZE = 10;
    private int mHistoriesMaxSize = DEFAULT_HISTORIES_SIZE;
    /**
     * 添加历史记录
     * @param history
     */
    private void saveHistory(String history){
        Histories histories = mJsonCacheUtil.getValue(KEY_HISTORIES, Histories.class);
        //如果已经存在,就干掉,然后再添加
        List<String> historiesList = null;
        if (histories != null && histories.getHistories() != null) {
            historiesList = histories.getHistories();
            if (historiesList.contains(history)) {
                historiesList.remove(history);
            }
        }
        //去重完成
        //处理没有数据的情况
        if (historiesList == null) {
            historiesList = new ArrayList<>();
        }
        if (histories == null) {
            histories = new Histories();
        }
        histories.setHistories(historiesList);
        //对个数进行限制
        if (historiesList.size() > mHistoriesMaxSize) {
            historiesList = historiesList.subList(0,mHistoriesMaxSize);
        }
        //添加记录
        historiesList.add(history);
        //保存记录
        mJsonCacheUtil.saveCache(KEY_HISTORIES,historiesList);
    }
    @Override
    public void doSearch(String keyword) {
        if (mCurrentKeyword ==null || !mCurrentKeyword.endsWith(keyword)){
            this.saveHistory(keyword);
            this.mCurrentKeyword = keyword;
        }
        //更新UI状态
        if (mSearchViewCallback != null) {
            mSearchViewCallback.onLoading();
        }
        Call<SearchResult> task = mApi.doSearch(mCurrentPage,keyword);
        task.enqueue(new Callback<SearchResult>() {
            @Override
            public void onResponse(Call<SearchResult> call, Response<SearchResult> response) {
                int code = response.code();
                LogUtils.d(SearchPresenter.this,"do seaarch result code -->" + code);
                if (code == HttpsURLConnection.HTTP_OK){
                    handleSearchResult(response.body());
                }else {
                    onError();
                }
            }
            @Override
            public void onFailure(Call<SearchResult> call, Throwable t) {
                t.printStackTrace();
                onError();
            }
        });
    }
    private void onError() {
        if (mSearchViewCallback != null) {
            mSearchViewCallback.onError();
        }
    }
    private void handleSearchResult(SearchResult result) {
        if (mSearchViewCallback != null){
            if (isResultEmpty(result)){
                //数据为空
                mSearchViewCallback.onEmpty();
            }else {
                mSearchViewCallback.onSearchSuccess(result);
            }
        }
    }
    private boolean isResultEmpty(SearchResult result){
        try {
            return result == null || result.getData().getTbk_dg_material_optional_response().getResult_list().getMap_data().size() == 0 ;
        }catch (Exception e){
            return false;
        }
    }
    @Override
    public void research() {
        if (mCurrentKeyword == null) {
            if (mSearchViewCallback != null) {
                mSearchViewCallback.onEmpty();
            }else {
                //可以重新搜素
                this.doSearch(mCurrentKeyword);
            }
        }
    }
    @Override
    public void loaderMore() {
        mCurrentPage++;
        //进行搜素
        if (mCurrentKeyword == null){
            if (mSearchViewCallback != null) {
                mSearchViewCallback.onEmpty();
            }else {
                //做搜素的事情
                doSearchMore();
            }
        }
    }
    private void doSearchMore() {
        Call<SearchResult> task = mApi.doSearch(mCurrentPage,mCurrentKeyword);
        task.enqueue(new Callback<SearchResult>() {
            @Override
            public void onResponse(Call<SearchResult> call, Response<SearchResult> response) {
                int code = response.code();
                LogUtils.d(SearchPresenter.this,"doSearchMore result code -->" + code);
                if (code == HttpsURLConnection.HTTP_OK){
                    handleMoreSearchResult(response.body());
                }else {
                    onLoaderMoreError();
                }
            }
            @Override
            public void onFailure(Call<SearchResult> call, Throwable t) {
                t.printStackTrace();
                onLoaderMoreError();
            }
            });
        }
    /**
     * 处理加载更多的结果
     * @param result
     */
    private void handleMoreSearchResult(SearchResult result) {
        if (mSearchViewCallback != null){
            if (isResultEmpty(result)){
                //数据为空
                mSearchViewCallback.onMoreLoadedEmpty();
            }else {
                mSearchViewCallback.onMoreLoaded(result);
            }
        }
    }
    /**
     * 加载更多内容失败
     */
    private void onLoaderMoreError() {
        mCurrentPage--;
        if (mSearchViewCallback != null) {
            mSearchViewCallback.onMoreLoadedError();
        }
    }
    @Override
    public void getRecommendWords() {
        Call<SearchRecommend> task = mApi.getRecommendWords();
        task.enqueue(new Callback<SearchRecommend>() {
            @Override
            public void onResponse(Call<SearchRecommend> call, Response<SearchRecommend> response) {
                int code = response.code();
                LogUtils.d(SearchPresenter.this,"getRecommendWords result code -->" + code);
                if (code == HttpURLConnection.HTTP_OK){
                    //处理数据
                    if (mSearchViewCallback != null) {
                        mSearchViewCallback.onRecommendWordsLoaded(response.body().getData());
                    }
                }
            }
            @Override
            public void onFailure(Call<SearchRecommend> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
    @Override
    public void registerViewCallback(ISearchPageCallback callback) {
        this.mSearchViewCallback = callback;
    }
    @Override
    public void unregisterViewCallback(ISearchPageCallback callback) {
        this.mSearchViewCallback = null;
    }
}
兄弟,怎么解决的?我也遇到了这个问题,教教我
好兄弟,解决了吗,我有个朋友也有这个问题,麻烦详细回一下
Gson想要的是一个对象,但是给的却是一个数组?把Json字符串打印看一看?