【发布时间】:2015-12-24 23:16:43
【问题描述】:
您好,我正在尝试将以下 JSON 响应解析为列表
{"beers":[{"id":"1","nombre":"Larzobispa","localidad":"Alcorcon","provincia":"Madrid"},{"id":"2","nombre":"La Cibeles","localidad":"Madrid","provincia":"Madrid"}]}
我使用下面的代码
我的界面
public interface AbebeerApi {
@GET("/beer_app/beers")
List<SingleBeer> listBeers();
}
AbebeerApi 的实现
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("end_point_url")
.build();
AbebeerApi service = retrofit.create(AbebeerApi.class);
以及对已创建服务的调用
List<SingleBeer> beers = service.listBeers();
我在啤酒列表中没有得到任何回应。我的应用程序关闭,我在 logcat 中没有得到任何信息。
这是我的 POJO 类
public class SingleBeer {
int id;
String nombre;
String localidad;
String provincia;
//getters and setters
}
有人可以帮我吗? 提前谢谢你
编辑:我让你我的 php 代码以防万一它有问题
if (mysql_num_rows($result) > 0) {
// looping through all results
$response["beers"] = array();
while ($row = mysql_fetch_array($result)) {
$beers = array();
$beers["id"] = $row["id"];
$beers["nombre"] = $row["nombre"];
$beers["localidad"] = $row["localidad"];
$beers["provincia"] = $row["provincia"];
// push single product into final response array
array_push($response["beers"], $beers);
}
// success
$response["succes"] = 1;
// echoing JSON response
echo json_encode($response);
mysql_close($localhost);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No beers have found";
// echo no users JSON
echo json_encode($response);
mysql_close($localhost);
}
编辑 2:我用这篇文章解决了这个问题 https://stackoverflow.com/a/27698866/4669063
我创建了我在 setConverter 方法中添加到我的 RestAdapter 的 LenientGsonConverter 类
【问题讨论】:
-
看起来您需要在构建适配器时添加转换器。看到这个答案stackoverflow.com/a/32390665/1904517
-
感谢您的评论,但仍然无法使用该答案......
-
我只是更改了我的代码并使用改造 1.9.0 但仍然无法正常工作。如果我使用api.github.com/users,我可以获取列表中的所有用户,但是当我想获取我的 Json 时它不起作用。它具有相同的结构...[{"id":"1","nombre":"La arzobispa","localidad":"Alcorcon","provincia":"Madrid"},{"id":" 2","nombre":"La Cibeles","localidad":"马德里","provincia":"马德里"}]
标签: android arrays json retrofit pojo