Back to project page android-model-record.
The source code is released under:
MIT License
If you think the Android project android-model-record listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.fanpics.opensource.android.modelrecord; //w w w . j a v a2 s . c o m import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.lang.reflect.Type; import retrofit.client.Response; import retrofit.converter.ConversionException; import retrofit.converter.GsonConverter; public class ResponseParser<T> { private final Response response; private final Type type; private String dateFormat; public ResponseParser(Response response, Type type) { this.response = response; this.type = type; } public void setDateFormat(String dateFormat) { this.dateFormat = dateFormat; } public Result<T> parse() { try { final T object = parseFromResponse(); return new Result<>(response, object); } catch (ConversionException e) { throw new RuntimeException("Response body must match type passed in at constructor", e); } } @SuppressWarnings("unchecked") private T parseFromResponse() throws ConversionException { final Gson gson = getGson(); final GsonConverter converter = new GsonConverter(gson); return (T) converter.fromBody(response.getBody(), type); } public Gson getGson() { Gson gson; if (dateFormat != null) { gson = new GsonBuilder().setDateFormat(dateFormat).create(); } else { gson = new GsonBuilder().create(); } return gson; } }