【问题标题】:Retrofit manage No objects model list JSON改造管理无对象模型列表JSON
【发布时间】:2016-07-11 23:56:09
【问题描述】:

我正在使用 Retrofit lib 从我实现的 JSON Api Rest Service 中获取对象,在 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 然后管理错误?

【问题讨论】:

    标签: json rest error-handling retrofit2


    【解决方案1】:

    在你的方法中使用响应体而不是使用对象模型

    改造 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();
    }
    });
    

    【讨论】:

    • 对不起 SaravInfern,我就是这样做的,但是当我得到 json "message":"No Event" onResponse 函数没问题,我得到一个事件列表,其中只有一个对象,所有字段为空...都是因为即使没有事件,响应代码也是 200...我试图更改响应代码,但什么也没有...我正在使用 json api 插件 wordpress 和自定义控制器...你知道我该怎么做吗创建一个自定义 api 休息?
    • 我听不懂,请您说清楚,您面临的问题是什么?你用断点调试调用了吗??
    • 如果我用“无事件”检索 json,在 onReponse 回调中它会创建一个事件列表,其中只有一个元素的所有字段都为空,这会导致一个空点断点。如果json是否充满元素,我必须在onResponse中切换响应200,在这两种情况下,我的api都将检索200响应:如果有元素,则对象的arraylist被正确填充,如果没有,arraylist只有一个元素null 字段,这很令人沮丧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2019-06-03
    • 2013-09-10
    • 2020-07-16
    • 2023-02-07
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多