gpt4 book ai didi

json - 改造管理无对象模型列表JSON

转载 作者:行者123 更新时间:2023-12-03 08:51:56 24 4
gpt4 key购买 nike

我正在使用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,然后处理错误?

最佳答案

代替使用对象模型,在方法中使用Response主体

改造2.x中的示例

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

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

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

关于json - 改造管理无对象模型列表JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38305082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com