android - How to handle returns can be either OBJECT or ARRAY with Retrofit 2? -
i'm having issue api return either array or object, below data format:
object format:
{ "info":"no package", "time":"04-20-2016" }
array format:
[ { "package_id":"1234", "from":"ca", "arrive_time":"05-02-2016" }, { "package_id":"4567", "from":"de", "arrive_time":"05-04-2016" } ]
after checked posts (custom converter retrofit 2, multiple converters retrofit 2), have clue should dealed gson deserializer or custom converter, case seems little different. how deal it? in advance.
update: change example more proper one.
chaosphinx
i agree problem different posts have referenced.
your first json has informations , exception in request , second 1 returned when request successful. can suggest check response http code before convert json. if code 202 (java.net.httpurlconnection.http_ok), because request successful , api return second json, convert object. if code else because exception has happened , should deal in different way.
an example:
response<list<myobject>> response = myresource.mymethod().execute(); switch (response.code()) { case http_ok: return response.body(); default: //ops! request has failed! }
Comments
Post a Comment