Skip to content

Instantly share code, notes, and snippets.

View parthdave93's full-sized avatar
🏠
Working from home

Parth Dave parthdave93

🏠
Working from home
View GitHub Profile
@parthdave93
parthdave93 / DateDeserializer.java
Last active March 6, 2017 05:36
Parse dates on Api response level
public class DateDeserializer implements JsonDeserializer<MyDate> {
//if not epoch time from server response
public static SimpleDateFormat serverFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
public static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
@Override
public MyDate deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
String date = element.getAsString();
//if epoch sent then create date object directly new Date(time*1000)
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));//the timezones you want
@parthdave93
parthdave93 / ApiCallActivity.java
Last active March 1, 2017 05:44
Retrofit will throw error for 401,402 type server response if we want to catch that in RxJava, Things to do for Retrofit RxJava Error Handling -> Check OperatorMapResponseToBodyOrError.java for error handling other classes are just to instanciate the Rxjava adapter on proejct level
if (throwable instanceof HttpException) {
Response response = ((HttpException) throwable).response();
RetrofitError commonResponse = null;
if (response != null) {
Gson gson = new Gson();
try {
commonResponse = gson.fromJson(response.errorBody().string(), RetrofitError.class);
CommonUtilities.showAlertDialog(context, commonResponse.getMessage());
} catch (IOException e) {
e.printStackTrace();