Last active
August 29, 2015 14:18
-
-
Save SeongUgJung/31fba4a546d7873e7d7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GsonBuilder gson = new GsonBuilder(); | |
gson.registerTypeAdapter(ResponseMessage.class, new UsingTimeDataTypeAdapter()); | |
return gson.create().fromJson(result, ResponseMessage.class); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TimeDataTypeAdapter extends TypeAdapter<ResponseMessage> { | |
@Override | |
public void write(JsonWriter out, ResponseMessage valueList) throws IOException { | |
// 처리 하지 않음 | |
} | |
@Override | |
public ResponseMessage read(JsonReader in) throws IOException { | |
JsonParser parser = new JsonParser(); | |
JsonObject bodyObject = parser.parse(in).getAsJsonObject().getAsJsonObject("body"); | |
String result = bodyObject.get("result").getAsString(); | |
String message = bodyObject.get("message").getAsString(); | |
if (!TextUtils.equals("200", result)) { | |
MessageBody body = new MessageBody(new HashMap<Object, Object>()); | |
body.setMessage(message); | |
body.setResult(result); | |
return new ResponseMessage(body); | |
} | |
// 그래프에 표시될 최고 값 | |
int maxTime = 0; | |
// 전체 자녀 사용자 7일간 평균 사용시간 파싱 | |
JsonArray timeAverageList = bodyObject.getAsJsonObject("data").getAsJsonArray("timeAverageList"); | |
ArrayList<TimeTO> timeAverageTOs = new ArrayList<TimeTO>(); | |
for (int idx = 0, size = timeAverageList.size(); idx < size; ++idx) { | |
TimeTO timeTO = new TimeTO(); | |
JsonObject tempTimeObject = timeAverageList.get(idx).getAsJsonObject(); | |
String date = tempTimeObject.get("date").getAsString(); | |
int time = tempTimeObject.get("time").getAsInt(); | |
if (time > maxTime) { | |
maxTime = time; | |
} | |
timeTO.setDate(date); | |
timeTO.setTime(time); | |
timeAverageTOs.add(timeTO); | |
} | |
HashMap<Object, Object> data = new HashMap<Object, Object>(); | |
data.put("timeAverageList", timeAverageTOs); | |
data.put("maxTime", maxTime); | |
MessageBody body = new MessageBody(data); | |
body.setMessage(message); | |
body.setResult(result); | |
return new ResponseMessage(body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment