预期开始数组但在 Retrofit 调用中是开始对象
Expected begin array but was begin object in Retrofit call
一开始我必须说我是Android中的新手。我想用 Retrofit 进行调用,但调用时出现错误:Expected begin array but was begin object
.
我正在尝试解析 result
,然后在 RecyclerView
中显示它。下面是我的适配器、activity、POJO 和接口文件。在此先感谢您的帮助。
例子
public class Example {
@SerializedName("pagination")
@Expose
private Pagination pagination;
@SerializedName("results")
@Expose
private List<Result> results = null;
public Pagination getPagination() {
return pagination;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
}
结果
public class Result {
@SerializedName("country")
@Expose
private String country;
@SerializedName("year")
@Expose
private String year;
@SerializedName("format")
@Expose
private List<String> format = null;
@SerializedName("label")
@Expose
private List<String> label = null;
@SerializedName("type")
@Expose
private String type;
@SerializedName("genre")
@Expose
private List<String> genre = null;
@SerializedName("style")
@Expose
private List<String> style = null;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("barcode")
@Expose
private List<String> barcode = null;
@SerializedName("master_id")
@Expose
private Integer masterId;
@SerializedName("master_url")
@Expose
private String masterUrl;
@SerializedName("uri")
@Expose
private String uri;
@SerializedName("catno")
@Expose
private String catno;
@SerializedName("title")
@Expose
private String title;
@SerializedName("thumb")
@Expose
private String thumb;
@SerializedName("cover_image")
@Expose
private String coverImage;
@SerializedName("resource_url")
@Expose
private String resourceUrl;
@SerializedName("community")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public List<String> getFormat() {
return format;
}...
Activity
private void GetSongInfo(){
try{
SongsApi songsApi= SongsClient.getRetrofitClient().create(SongsApi.class);
Call<List<Result>> call= songsApi.getSongsExampleInfo(3,randomResultPages,"pop",KEY,SECRET);
call.enqueue(new Callback<List<Result>>() {
@Override
public void onResponse(Call<List<Result>> call, Response<List<Result>> response) {
if(response.isSuccessful()& response.body()!=null){
songList= new ArrayList<>(response.body());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(HappySongActivity.this);
recyclerView.setLayoutManager(layoutManager);
SongAdapter= new SongAdapter(songList,SongActivity.this);
recyclerView.setAdapter(happySongAdapter);
}
}
@Override
public void onFailure(Call<List<Result>> call, Throwable t) {
Toast.makeText(getBaseContext(), "Server Error- Couldn't load data, Please try again"+t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}catch (Exception ex){
Toast.makeText(this,"Something was wrong"+ex.getMessage(),Toast.LENGTH_SHORT).show();
}
适配器
public class SongAdapter extends RecyclerView.Adapter<SongAdapter.CustomViewHolder> {
private ArrayList<Result> dataList;
private Context context;
public SongAdapter(ArrayList<Result> dataList, Context context){
this.dataList = dataList;
this.context=context;
}...
通话
@GET("/database/search")
Call<List<Result>> getSongsExampleInfo(@Query("per_page") Integer per_page, @Query("page") Integer page, @Query("genre") String genre, @Query("key") String key, @Query("secret") String secret);
您的改装服务方法具有 return 类型 Call<List<Result>>
- 这意味着改装将尝试将响应解析为 json 对象列表。但是 API 是 return 单个对象,而不是列表。
检查 API 实际上 return 是什么。如果它实际上是 returning 单个 Result
对象,请将方法声明更改为:
Call<Result> getSongsExampleInfo(....
一开始我必须说我是Android中的新手。我想用 Retrofit 进行调用,但调用时出现错误:Expected begin array but was begin object
.
我正在尝试解析 result
,然后在 RecyclerView
中显示它。下面是我的适配器、activity、POJO 和接口文件。在此先感谢您的帮助。
例子
public class Example {
@SerializedName("pagination")
@Expose
private Pagination pagination;
@SerializedName("results")
@Expose
private List<Result> results = null;
public Pagination getPagination() {
return pagination;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
}
结果
public class Result {
@SerializedName("country")
@Expose
private String country;
@SerializedName("year")
@Expose
private String year;
@SerializedName("format")
@Expose
private List<String> format = null;
@SerializedName("label")
@Expose
private List<String> label = null;
@SerializedName("type")
@Expose
private String type;
@SerializedName("genre")
@Expose
private List<String> genre = null;
@SerializedName("style")
@Expose
private List<String> style = null;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("barcode")
@Expose
private List<String> barcode = null;
@SerializedName("master_id")
@Expose
private Integer masterId;
@SerializedName("master_url")
@Expose
private String masterUrl;
@SerializedName("uri")
@Expose
private String uri;
@SerializedName("catno")
@Expose
private String catno;
@SerializedName("title")
@Expose
private String title;
@SerializedName("thumb")
@Expose
private String thumb;
@SerializedName("cover_image")
@Expose
private String coverImage;
@SerializedName("resource_url")
@Expose
private String resourceUrl;
@SerializedName("community")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public List<String> getFormat() {
return format;
}...
Activity
private void GetSongInfo(){
try{
SongsApi songsApi= SongsClient.getRetrofitClient().create(SongsApi.class);
Call<List<Result>> call= songsApi.getSongsExampleInfo(3,randomResultPages,"pop",KEY,SECRET);
call.enqueue(new Callback<List<Result>>() {
@Override
public void onResponse(Call<List<Result>> call, Response<List<Result>> response) {
if(response.isSuccessful()& response.body()!=null){
songList= new ArrayList<>(response.body());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(HappySongActivity.this);
recyclerView.setLayoutManager(layoutManager);
SongAdapter= new SongAdapter(songList,SongActivity.this);
recyclerView.setAdapter(happySongAdapter);
}
}
@Override
public void onFailure(Call<List<Result>> call, Throwable t) {
Toast.makeText(getBaseContext(), "Server Error- Couldn't load data, Please try again"+t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}catch (Exception ex){
Toast.makeText(this,"Something was wrong"+ex.getMessage(),Toast.LENGTH_SHORT).show();
}
适配器
public class SongAdapter extends RecyclerView.Adapter<SongAdapter.CustomViewHolder> {
private ArrayList<Result> dataList;
private Context context;
public SongAdapter(ArrayList<Result> dataList, Context context){
this.dataList = dataList;
this.context=context;
}...
通话
@GET("/database/search")
Call<List<Result>> getSongsExampleInfo(@Query("per_page") Integer per_page, @Query("page") Integer page, @Query("genre") String genre, @Query("key") String key, @Query("secret") String secret);
您的改装服务方法具有 return 类型 Call<List<Result>>
- 这意味着改装将尝试将响应解析为 json 对象列表。但是 API 是 return 单个对象,而不是列表。
检查 API 实际上 return 是什么。如果它实际上是 returning 单个 Result
对象,请将方法声明更改为:
Call<Result> getSongsExampleInfo(....