rx java retrofit 2 error handling

后端 未结 3 1679
無奈伤痛
無奈伤痛 2020-12-31 23:36

I\'m using retrofit 2 along with rx java

Situation: the app sends some request, then i get the response in json-format that is automatically converted to User dto,

3条回答
  •  攒了一身酷
    2021-01-01 00:11

    I believe in the case you mentioned you will just enter into your onError handling, because retrofit will fail to deserialize your response, as it's not formatted as a List. You could potentially handle your case through that based off of the exception type.

    If you can't alter the api to return consistent response types, you will have to look into using TypedInput, and possibly a converter.

    Additionally, while it may not be completely relevant/overkill to the situation at hand, TypeAdapters bear mentioning. They'll let you determine how retrofit deserializes gson on a per class basis.

    Gson gson = new GsonBuilder()
        .registerTypeAdapter(MyClass.class, new MyAdapter())
        .create();
    
    RestAdapter adapter = new RestAdapter.Builder()
        .setConverter(new GsonConverter(gson))
        .build();
    

提交回复
热议问题