改造管理没有对象模型列表JSON

时间:2016-07-11 10:51:33

标签: json rest error-handling retrofit2

我使用Retrofit lib从JSON Api Rest服务中获取对象我在PHP中实现了简单查询和回显。

这是我通常用于在我的Android APP中创建对象的JSON:

[  
   {  
      "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"
   }
]

如果对象列表为空,我需要像这样管理错误和JSON:

[  
   {  
      "status":"0",
      "message":"No Event"
   }
]

对于实施,我使用了这个 https://futurestud.io/blog/retrofit-getting-started-and-android-client

目前,如果没有事件(对象模型),我会检索200响应代码和json。 我该如何管理这种情况?我是否要更改Api Rest Response以给我404或400然后管理错误?

1 个答案:

答案 0 :(得分:0)

而不是使用对象模型在方法中使用Response body

改造2.x中的示例

 public interface RequestService{
    @GET("/users/{user}")
    Call<ResponseBody> getLogin(@Field("userId") String, @Field("password") String);
}

在您的活动中

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 your json here
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onFailure(Throwable t) {
    e.printStackTrace();
}
});