Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GsonConvert implements Converter {
- private static final Gson GSON = new Gson();
- @Override
- public <S, F> SimpleResponse<S, F> convert(Type succeed, Type failed, Response response,
- boolean fromCache) throws Exception {
- S succeedData = null;
- F failedData = null;
- String JSON = response.body().string();
- JSON = Net.decrypt(JSON);
- if (Net.printResponse) {
- LogCat.r("JSON", JSON);
- } else {
- LogCat.json("响应体", JSON);
- }
- int code = response.code();
- // 解析服务器错误码
- if (code >= 200 && code < 300) {
- JSONObject jsonObject = null;
- try {
- jsonObject = new JSONObject(JSON);
- } catch (JSONException e) {
- e.printStackTrace();
- failedData = (F) "无法解析错误信息";
- }
- // 解析后台自定义错误码
- int customCode = jsonObject.optInt("code");
- if (customCode == 1) {
- {
- try {
- succeedData = GSON.fromJson(JSON, succeed);
- } catch (JsonSyntaxException e) {
- e.printStackTrace();
- failedData = (F) "无法解析数据";
- }
- }
- } else {
- failedData = (F) jsonObject.optString("msg");
- code = customCode;
- }
- } else if (code >= 400 && code < 500) {
- failedData = (F) "发生异常错误";
- } else if (code >= 500) {
- failedData = (F) "服务器开小差啦";
- }
- return SimpleResponse.<S, F>newBuilder()
- .code(code)
- .headers(response.headers())
- .fromCache(fromCache)
- .succeed(succeedData)
- .failed(failedData)
- .build();
- }
- }
Add Comment
Please, Sign In to add comment