简体   繁体   中英

Handling different POJOs from different JSON API responses

I have a REST API which returns JSON to its calls. However, depending on the call I perform, the answer varies. Using Gson will result in a different POJO object for each API response.

Considering I have an IntentService who handles all my GET requests, and I only pass it an URL in the intent, what would be the best way to handle the different responses in the IntentService ?

I've been messing around with JSONs in Android for a while, but I can't seem to find an elegant/smart solution for this issue. I've been creating an enum to differentiate the possible API requests, and passing the selected enum value in the intent, in order for the IntentService to choose the action to perform using a switch statement.

Thank you.

If there is a common business logic for all the resulting different POJOs you get, then it would be nice to try to use the same Class, and for this, to try to customize the GSON mapping strategy.

If you don't really have a common logic, then you can consider your GSON objects as DTOs, and then your "mapping" to the business Classes is done on a lower layer (And you have here a lot of ways to do it, including Dozer).

For a more specific answer, more details or examples for your project would help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM