我想解析這個json波紋管,到名爲AlbumResponse
的對象裏面有另外兩個對象Album
和PaginationInfo
。改造版本2.0無法解析json和兩種不同的數據類型在改進2.0
[
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
Album類
public class Album {
long id;
String name;
@SerializedName("artist_name")
String artistName;
String image;
}
PaginationInfo類
public class PaginationInfo {
int page;
int pagerMax;
int nextPage;
int itemsTotal;
}
AlbumResponse中,具有上述內部兩個類,並且Album
是List
public class AlbumResponse {
public List<Album> albums;
public PaginationInfo paginationInfo;
}
請求
Call<AlbumResponse> responseCall = albumService.features();
responseCall.enqueue(new Callback<AlbumResponse>() {
@Override
public void onResponse(Response<AlbumResponse> response, Retrofit retrofit) {
if(response.isSuccess()) {
AlbumResponse albumResponse = response.body();
PaginationInfo paginationInfo = albumResponse.getPaginationInfo();
}
System.out.println();
}
@Override
public void onFailure(Throwable t) {
System.out.println(t.getMessage());
}
});
接口
public interface AlbumService {
@GET("/features/")
Call<AlbumResponse> features();
}
的問題是,即時得到一個Throwable
包含:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:預計BEGIN_OBJECT,但在BEGIN_ARRAY第1行第2列路徑$`
請有人可以幫助我,我沒有找到任何答案在stackoverflow。謝謝。
是的,它的工作就像一個魅力,問題是在'JSON',而不是'RETROFIT'。這是一個begginer的錯誤。謝謝 – diogojme