rest - Retrofit manage No objects model list JSON -
i'm using retrofit lib objects json api rest service implemented, simple query , echo in php.
this json use create objects in android app:
[ { "event_id":"1", "event_name":"lungo il tevere", "event_image_url":"https:\/\/www.dayroma.it\/wp-content\/uploads\/2016\/06\/event_fb-9400.jpg", "event_content":"lungo il tevere roma 2016, bancarelle sulle banchine del tevere", "event_owner":"1", "event_start_time":"22:00:00", "event_end_time":"03:00:00", "event_all_day":"0", "event_start_date":"2016-07-05", "event_end_date":"2016-07-06", "location_name":"lungotevere", "location_owner":"1", "location_address":"piazza navona 2", "location_town":"roma", "location_state":null, "location_postcode":null, "location_region":null, "location_country":"it", "location_latitude":"41.897785", "location_longitude":"12.472971" }, { "event_id":"3", "event_name":"black mountain + soviet soviet", "event_image_url":"https:\/\/www.dayroma.it\/wp-content\/uploads\/2016\/04\/timthumb.jpeg", "event_content":"affrontare la psichedelia granitica dei black mountain", "event_owner":"1", "event_start_time":"20:00:00", "event_end_time":"00:00:00", "event_all_day":"0", "event_start_date":"2016-07-05", "event_end_date":"2016-07-06", "location_name":"villa ada", "location_owner":"1", "location_address":"villa ada ", "location_town":"roma", "location_state":null, "location_postcode":null, "location_region":null, "location_country":"it", "location_latitude":"41.932831", "location_longitude":"12.501247" }, { "event_id":"7", "event_name":"test 4", "event_image_url":"", "event_content":"05test 4", "event_owner":"1", "event_start_time":"01:00:00", "event_end_time":"05:00:00", "event_all_day":"0", "event_start_date":"2016-07-06", "event_end_date":"2016-07-06", "location_name":"villa ada", "location_owner":"1", "location_address":"villa ada ", "location_town":"roma", "location_state":null, "location_postcode":null, "location_region":null, "location_country":"it", "location_latitude":"41.932831", "location_longitude":"12.501247" } ]
i need manage errors , json this, if objects list empty:
[ { "status":"0", "message":"no event" } ]
for implementation used https://futurestud.io/blog/retrofit-getting-started-and-android-client
at moment, if there no events ( object model ), retrive 200 response code , json. how can manage scenario? have change api rest response give me 404 or 400 , manage error?
instead of using object model use response body in method
example in retrofit 2.x
public interface requestservice{ @get("/users/{user}") call<responsebody> getlogin(@field("userid") string, @field("password") string); }
in activity
requestservice service = retrofit.create(requestservice.class); call<responsebody> result = service.getlogin(username,password); result.enqueue(new callback<responsebody>() { @override public void onresponse(response<responsebody> response) { try { system.out.println(response.body().string());//parse json here } catch (ioexception e) { e.printstacktrace(); } } @override public void onfailure(throwable t) { e.printstacktrace(); } });
Comments
Post a Comment