Skip to content Skip to sidebar Skip to footer

Broken Server Response Handling With Moshi

The expected json response from server should be : { 'teacher': { '123': { '_id': '389', 'name': 'test_fast_teacher1' } } } Server returned json with this

Solution 1:

How about something like this?

Moshi moshi = new Moshi.Builder()
    .add(DefaultOnDataMismatchAdapter.newFactory(Teacher.class, null))
    .build();

JsonAdapter<Teacher> adapter = moshi.adapter(Teacher.class);

Teacher teacher = adapter.fromJson(json);
// teacher == null

where DefaultOnDataMismatchAdapter is Jesse's code you can copy into your code base.

When the Teacher type comes back in an unexpected format that would produce a JsonDataException, it will default back to your set value (in this case, null).

Post a Comment for "Broken Server Response Handling With Moshi"