MovieMsgImpl.java
7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) 2016 wugian
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xgimi.gimicinema.model;
import android.text.TextUtils;
import com.gimi.common.cinema.model.MovieMessage;
import com.gimi.common.cinema.model.QueryBean;
import com.gimi.common.cinema.model.WrongMsg;
import com.gimi.common.cinema.utils.FileReadUtils;
import com.gimi.common.cinema.utils.OkHttpClientManager;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Request;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* Created by 李攀 on 2016/9/20
*/
public class MovieMsgImpl implements IMovieMsgModel {
private static final String QUERY_DOUBAN_ID_URL =
"https://api.douban.com/v2/movie/search?count=3&q=";
private static final String GET_MOVIE_MESSAGE_URL =
"https://api.douban.com/v2/movie/subject/";
@Override
public void queryDoubanId(String name, String idPath,
final OnGetDoubanIdListener listener) {
String doubanId = null;
File nameId = new File(idPath);
if (nameId.exists()) {
String id = FileReadUtils.readDoubanId(idPath, name);
if (!TextUtils.isEmpty(id)) {
String[] split = id.split("#");
if (split.length >= 2) {
doubanId = split[1];
}
}
}
if (!TextUtils.isEmpty(doubanId)) {
listener.onGetDoubanIdSuccess(doubanId);
return;
}
OkHttpClientManager.getAsyn(QUERY_DOUBAN_ID_URL + name,
new OkHttpClientManager.ResultCallback<String>() {
@Override
public void onError(Request request, Exception e) {
listener.onGetDoubanIdFailure(new WrongMsg());
}
@Override
public void onResponse(String response) {
try {
Gson gson = new Gson();
WrongMsg wrongMsg = gson.fromJson(response,
new TypeToken<WrongMsg>() {
}.getType());
if (wrongMsg.getCode() != 0) {
listener.onGetDoubanIdFailure(wrongMsg);
} else {
QueryBean bean = gson.fromJson(response,
new TypeToken<QueryBean>() {
}.getType());
List<QueryBean.SubjectsEntity> subjects =
bean.getSubjects();
if (subjects.size() < 1) {
wrongMsg.setMsg("获取结果内容为空");
listener.onGetDoubanIdFailure(wrongMsg);
return;
}
QueryBean.SubjectsEntity subjectsEntity =
subjects.get(0);
String id = subjectsEntity.getId();
listener.onGetDoubanIdSuccess(id);
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
WrongMsg wrongMsg = new WrongMsg();
wrongMsg.setMsg(e.getMessage());
listener.onGetDoubanIdFailure(wrongMsg);
}
}
});
}
@Override
public void getMovieMessage(String doubanId,
final OnGetMovieMsgListener listener) {
OkHttpClientManager.getAsyn(GET_MOVIE_MESSAGE_URL + doubanId,
new OkHttpClientManager.ResultCallback<String>() {
@Override
public void onError(Request request, Exception e) {
WrongMsg wrongMsg = new WrongMsg();
wrongMsg.setMsg(e.getMessage());
listener.onGetMovieMsgFailure(wrongMsg);
}
@Override
public void onResponse(String response) {
try {
WrongMsg wrongMsg;
MovieMessage result;
Gson gson = new Gson();
wrongMsg = gson.fromJson(response,
new TypeToken<WrongMsg>() {
}.getType());
if (wrongMsg.getCode() != 0) {
if (listener != null)
listener.onGetMovieMsgFailure(wrongMsg);
} else {
result = gson.fromJson(response,
new TypeToken<MovieMessage>() {
}.getType());
if (listener != null)
listener.onGetMovieMsgSuccess(result);
}
} catch (JsonSyntaxException e) {
WrongMsg wrongMsg = new WrongMsg();
wrongMsg.setMsg(e.getMessage());
listener.onGetMovieMsgFailure(wrongMsg);
e.printStackTrace();
}
}
});
}
@Override
public void getMovieMessage(String doubanId, String name, String idPath,
final OnGetMovieMsgListener listener) {
if (!TextUtils.isEmpty(doubanId)) {
File nameId = new File(idPath);
if (nameId.exists()) {
String id = FileReadUtils.readDoubanId(idPath, name);
if (!TextUtils.isEmpty(id)) {
String[] split = id.split("#");
if (split.length >= 2) {
doubanId = split[1];
}
}
}
getMovieMessage(doubanId, listener);
} else {
queryDoubanId(name, idPath, new OnGetDoubanIdListener() {
@Override
public void onGetDoubanIdSuccess(String id) {
getMovieMessage(id, listener);
}
@Override
public void onGetDoubanIdFailure(WrongMsg msg) {
listener.onGetMovieMsgFailure(msg);
}
});
}
}
@Override
public void saveEncryptMessage(String path, String content,
OnSaveMovieMsgListener listener) {
try {
FileReadUtils.writeDateByEncrypt(path, content);
listener.onSaveMovieMsgSuccess();
} catch (IOException e) {
e.printStackTrace();
listener.onSaveMovieMsgFailure(e);
}
}
interface OnGetDoubanIdListener {
void onGetDoubanIdSuccess(String id);
void onGetDoubanIdFailure(WrongMsg msg);
}
public interface OnGetMovieMsgListener {
void onGetMovieMsgSuccess(MovieMessage mm);
void onGetMovieMsgFailure(WrongMsg msg);
}
public interface OnSaveMovieMsgListener {
void onSaveMovieMsgSuccess();
void onSaveMovieMsgFailure(Exception e);
}
}