Skip to content

Commit bad49b3

Browse files
author
Tneciv
committed
enable files cached
1 parent 1f25c50 commit bad49b3

File tree

6 files changed

+72
-32
lines changed

6 files changed

+72
-32
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ dependencies {
6464
compile 'com.daimajia.androidanimations:library:1.1.3@aar'
6565
compile 'com.nineoldandroids:library:2.4.0'
6666
compile 'net.yslibrary.licenseadapter:licenseadapter:1.2.1'
67+
compile 'com.google.guava:guava-io:r03'
6768
}

app/src/main/java/com/tneciv/zhihudaily/home/presenter/INewsPresenter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
*/
77
public interface INewsPresenter {
88
void requestUrl(String url);
9+
10+
void parseJsonOfHots(String responseCallback);
11+
12+
void parseJsonOfNews(String callback);
913
}

app/src/main/java/com/tneciv/zhihudaily/home/presenter/NewsPresenterCompl.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.tneciv.zhihudaily.home.presenter;
22

33
import android.content.Context;
4-
import android.util.Log;
54

65
import com.google.gson.Gson;
76
import com.google.gson.JsonElement;
@@ -57,45 +56,56 @@ public void onFailure(Call call, IOException e) {
5756

5857
@Override
5958
public void onResponse(Call call, Response response) throws IOException {
60-
Gson gson = new Gson();
59+
6160

6261
if (url.equals(ZhihuApi.NEWS_LATEST) || url.contains(ZhihuApi.NEWS_HISTORY) || url.contains(ZhihuApi.THEME_NEWS_LIST)) {
6362
String callback = response.body().string();
64-
Log.d("NewsPresenter callback", callback);
6563
new CacheUtil(mContext).cacheFiles(url, callback);
66-
Type type = new TypeToken<List<NewsEntity>>() {
67-
}.getType();
68-
List<NewsEntity> newsEntities = null;
69-
try {
70-
JsonElement jsonElement = new JsonParser().parse(callback).getAsJsonObject().get("stories");
71-
newsEntities = gson.fromJson(jsonElement, type);
72-
EventBus.getDefault().post(new HomeEventEntity.NewEntityList(newsEntities));
73-
} catch (JsonSyntaxException | IllegalStateException | NullPointerException e) {
74-
e.printStackTrace();
75-
ErrorEntity entity = new ErrorEntity("服务器返回数据异常", "server error");
76-
EventBus.getDefault().post(entity);
77-
}
64+
parseJsonOfNews(callback);
7865
}
7966

8067
if (url.equals(ZhihuApi.NEWS_HOT)) {
8168
String responseCallback = response.body().string();
82-
Type type = new TypeToken<List<HotEntity>>() {
83-
}.getType();
84-
List<HotEntity> hotEntities = null;
85-
try {
86-
JsonElement jsonElement = new JsonParser().parse(responseCallback).getAsJsonObject().get("recent");
87-
hotEntities = gson.fromJson(jsonElement, type);
88-
EventBus.getDefault().post(new HomeEventEntity.HotEntityList(hotEntities));
89-
} catch (JsonSyntaxException | IllegalStateException | NullPointerException e) {
90-
e.printStackTrace();
91-
ErrorEntity entity = new ErrorEntity("服务器返回数据异常", "server error");
92-
EventBus.getDefault().post(entity);
93-
}
94-
69+
new CacheUtil(mContext).cacheFiles(ZhihuApi.NEWS_HOT, responseCallback);
70+
parseJsonOfHots(responseCallback);
9571
}
9672

9773
}
9874
});
9975
}
10076

77+
@Override
78+
public void parseJsonOfHots(String responseCallback) {
79+
Type type = new TypeToken<List<HotEntity>>() {
80+
}.getType();
81+
List<HotEntity> hotEntities = null;
82+
try {
83+
JsonElement jsonElement = new JsonParser().parse(responseCallback).getAsJsonObject().get("recent");
84+
Gson gson = new Gson();
85+
hotEntities = gson.fromJson(jsonElement, type);
86+
EventBus.getDefault().post(new HomeEventEntity.HotEntityList(hotEntities));
87+
} catch (JsonSyntaxException | IllegalStateException | NullPointerException e) {
88+
e.printStackTrace();
89+
ErrorEntity entity = new ErrorEntity("服务器返回数据异常", "server error");
90+
EventBus.getDefault().post(entity);
91+
}
92+
}
93+
94+
@Override
95+
public void parseJsonOfNews(String callback) {
96+
Type type = new TypeToken<List<NewsEntity>>() {
97+
}.getType();
98+
List<NewsEntity> newsEntities = null;
99+
try {
100+
JsonElement jsonElement = new JsonParser().parse(callback).getAsJsonObject().get("stories");
101+
Gson gson = new Gson();
102+
newsEntities = gson.fromJson(jsonElement, type);
103+
EventBus.getDefault().post(new HomeEventEntity.NewEntityList(newsEntities));
104+
} catch (JsonSyntaxException | IllegalStateException | NullPointerException e) {
105+
e.printStackTrace();
106+
ErrorEntity entity = new ErrorEntity("服务器返回数据异常", "server error");
107+
EventBus.getDefault().post(entity);
108+
}
109+
}
110+
101111
}

app/src/main/java/com/tneciv/zhihudaily/home/view/HotFragment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.tneciv.zhihudaily.home.model.HotEntity;
1111
import com.tneciv.zhihudaily.home.presenter.INewsPresenter;
1212
import com.tneciv.zhihudaily.home.presenter.NewsPresenterCompl;
13+
import com.tneciv.zhihudaily.utils.CacheUtil;
1314

1415
import java.util.ArrayList;
1516
import java.util.List;
@@ -48,6 +49,10 @@ public void setRecyclerLayout() {
4849

4950
@Override
5051
public void requestUrl() {
52+
String cache = new CacheUtil(getContext()).loadCache(ZhihuApi.NEWS_HOT);
53+
if (!cache.equals("")) {
54+
iNewsPresenter.parseJsonOfHots(cache);
55+
}
5156
iNewsPresenter.requestUrl(ZhihuApi.NEWS_HOT);
5257
}
5358

app/src/main/java/com/tneciv/zhihudaily/home/view/NewsFragmnt.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.tneciv.zhihudaily.home.model.NewsEntity;
1212
import com.tneciv.zhihudaily.home.presenter.INewsPresenter;
1313
import com.tneciv.zhihudaily.home.presenter.NewsPresenterCompl;
14+
import com.tneciv.zhihudaily.utils.CacheUtil;
1415
import com.tneciv.zhihudaily.utils.view.DividerItemDecoration;
1516

1617
import java.util.ArrayList;
@@ -62,6 +63,10 @@ public void requestUrl() {
6263
url = this.getArguments().getString("themeIdUrl");
6364
}
6465
}
66+
String cache = new CacheUtil(getContext()).loadCache(url);
67+
if (!cache.equals("")) {
68+
iNewsPresenter.parseJsonOfNews(cache);
69+
}
6570
iNewsPresenter.requestUrl(url);
6671
}
6772

@@ -75,8 +80,4 @@ public void updateView(HomeEventEntity.NewEntityList entityList) {
7580
swipeRefresh.setRefreshing(false);
7681
}
7782

78-
private void cacheJson(List<NewsEntity> list) {
79-
80-
}
81-
8283
}

app/src/main/java/com/tneciv/zhihudaily/utils/CacheUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import android.content.Context;
44
import android.os.Environment;
55

6+
import com.google.common.io.CharStreams;
67
import com.jakewharton.disklrucache.DiskLruCache;
78

89
import java.io.File;
910
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.InputStreamReader;
1013
import java.io.OutputStream;
1114

1215
/**
@@ -70,4 +73,20 @@ public DiskLruCache getDiskLruCache(String type) {
7073
}
7174
return diskLruCache;
7275
}
76+
77+
public String loadCache(String key) {
78+
DiskLruCache diskLruCache = getDiskLruCache("json");
79+
try {
80+
DiskLruCache.Snapshot snapshot = diskLruCache.get(HashUtil.hashKeyForDisk(key));
81+
if (snapshot != null) {
82+
InputStream inputStream = snapshot.getInputStream(0);
83+
String s = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
84+
snapshot.close();
85+
return s;
86+
}
87+
} catch (IOException e) {
88+
e.printStackTrace();
89+
}
90+
return "";
91+
}
7392
}

0 commit comments

Comments
 (0)