你好我試圖解析成一個列表下面的JSON響應獲取JSON數組與改造
{"beers":[{"id":"1","nombre":"Larzobispa","localidad":"Alcorcon","provincia":"Madrid"},{"id":"2","nombre":"La Cibeles","localidad":"Madrid","provincia":"Madrid"}]}
林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
我創建將LenientGsonConverter類添加到我的RestAdapter的setConverter方法中
看起來您在構建適配器時需要添加轉換器。看到這個答案http://stackoverflow.com/a/32390665/1904517 – iagreen
謝謝你的評論,但仍無法與回答工作.... – Bruno
我只是改變我的代碼,並使用改進1.9.0,但仍然沒有工作。如果我使用https://api.github.com/users,我可以得到列表中的所有用戶,但是當我想讓我的Json不起作用時。它具有相同的結構...... [{ 「ID」: 「1」, 「農佈雷」: 「香格里拉arzobispa」, 「localidad」: 「艾科坎」, 「省報」: 「馬德里」},{ 「ID」:」 2" , 「農佈雷」: 「拉一同慶祝」, 「localidad」: 「馬德里」, 「省報」: 「馬德里」}] – Bruno