|
1 | 1 | package com.tneciv.zhihudaily.home.presenter; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
4 | | -import android.util.Log; |
5 | 4 |
|
6 | 5 | import com.google.gson.Gson; |
7 | 6 | import com.google.gson.JsonElement; |
@@ -57,45 +56,56 @@ public void onFailure(Call call, IOException e) { |
57 | 56 |
|
58 | 57 | @Override |
59 | 58 | public void onResponse(Call call, Response response) throws IOException { |
60 | | - Gson gson = new Gson(); |
| 59 | + |
61 | 60 |
|
62 | 61 | if (url.equals(ZhihuApi.NEWS_LATEST) || url.contains(ZhihuApi.NEWS_HISTORY) || url.contains(ZhihuApi.THEME_NEWS_LIST)) { |
63 | 62 | String callback = response.body().string(); |
64 | | - Log.d("NewsPresenter callback", callback); |
65 | 63 | 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); |
78 | 65 | } |
79 | 66 |
|
80 | 67 | if (url.equals(ZhihuApi.NEWS_HOT)) { |
81 | 68 | 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); |
95 | 71 | } |
96 | 72 |
|
97 | 73 | } |
98 | 74 | }); |
99 | 75 | } |
100 | 76 |
|
| 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 | + |
101 | 111 | } |
0 commit comments