尝试使用改装从 API 获取数据时出现此错误。
型号
public class Movie {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("image")
@Expose
private String image;
@SerializedName("link")
@Expose
private String link;
@SerializedName("description")
@Expose
private String description;
@SerializedName("category")
@Expose
private String category;
@SerializedName("actor")
@Expose
private String actor;
@SerializedName("director")
@Expose
private String director;
@SerializedName("manufacturer")
@Expose
private String manufacturer;
@SerializedName("duration")
@Expose
private String duration;
@SerializedName("year")
@Expose
private String year;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;
@SerializedName("views")
@Expose
private Integer views;
@SerializedName("type")
@Expose
private String type;
//get and set....
}
APIservice.class
@Headers("app_token:dCuW7UQMbdvpcBDfzolAOSGFIcAec11a")
@POST("training-movie/movie/list")
Call<List<Movie>> getMovie();
MainActivity
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(" http://dev.bsp.vn:8081/")
.addConverterFactory(GsonConverterFactory.create())
.client(oBuilder.build())
.build();
APIService service = retrofit.create(APIService.class);
Call<List<Movie>> call = service.getMovie();
call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
if(response.isSuccessful()){
Toast.makeText(MainActivity.this, ""+response.body().toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {
Toast.makeText(MainActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
我在这里得到错误,我想我不明白改造工作如何。有人可以告诉我更多相关信息。
更新:JSON
{
"error": false,
"code": 0,
"message": "",
"paging": {
"total_item": 31,
"per_page": 10,
"current_page": 1,
"total_pages": 4
},
"data": [
{
"id": 1,
"title": "DON'T BREATHE / SÁT NHÂN TRONG BÓNG TỐI",
"image": "http://dev.bsp.vn:8081/training-movie/upload/movie/sdasdasda.jpg",
"link": "https://www.youtube.com/watch?v=TRusW3VPLaI",
"description": "Ba tên trộm liều lĩnh đột nhập vào nhà một người đàn ông giàu có bị mù. Lũ trộm cho rằng bản thân sẽ vớ bở, thế nhưng chúng đã sai. Trong bóng tối, kẻ mù làm vua. Người đàn ông tưởng chừng yếu đuối nay lại trở thành ác quỷ đưa bọn chúng xuống địa ngục.",
"category": "Kinh dị, Hành động",
"actor": "Daniel Zovatto, Jane Levy",
"director": "Fede Alvarez",
"manufacturer": "Sony Pictures",
"duration": "88",
"year": "2016-08-30",
"created_at": "2016-08-30 15:45:37",
"updated_at": "0000-00-00 00:00:00",
"views": 896,
"type": "showing"
}
]
}
更新:型号
public class Data {
@SerializedName("error")
@Expose
private Boolean error;
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("paging")
@Expose
private Paging paging;
@SerializedName("data")
@Expose
private List<Movie> data = null;
public class Paging {
@SerializedName("total_item")
@Expose
private Integer totalItem;
@SerializedName("per_page")
@Expose
private Integer perPage;
@SerializedName("current_page")
@Expose
private Integer currentPage;
@SerializedName("total_pages")
@Expose
private Integer totalPages;
答案 0 :(得分:0)
创建一个名为response
的类public class Response {
public boolean error;
public int code;
public String message;
public List<Movie> data;
}
修改api服务类函数,使其看起来像这样
Call<Response> getMovie();
此处Response是您在上面创建的类。这应该有用。
P.S。您的应用令牌已被盗用,请考虑更改它,并且对于将来的事件,不要在堆栈溢出时发布任何敏感数据,如密钥,令牌等。
答案 1 :(得分:0)
尝试将Data类作为对象获取,如下所示
<强> ApiService 强>
@Headers("app_token:dCuW7UQMbdvpcBDfzolAOSGFIcAec11a")
@POST("training-movie/movie/list")
Call<Data> getMovie();
<强> MainActivity 强>
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(" http://dev.bsp.vn:8081/")
.addConverterFactory(GsonConverterFactory.create())
.client(oBuilder.build())
.build();
APIService service = retrofit.create(APIService.class);
Call<Data> call = service.getMovie();
call.enqueue(new Callback<Data>() {
@Override
public void onResponse(Call<Data> call, Response<Data> response) {
if(response.isSuccessful()){
Toast.makeText(MainActivity.this, ""+response.body().getData().toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Data> call, Throwable t) {
Toast.makeText(MainActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
然后像这样更改数据类
public class Data {
@SerializedName("error")
@Expose
private Boolean error;
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("paging")
@Expose
private Paging paging;
@SerializedName("data")
@Expose
private List<Movie> data = null;
public List<Movie> getData() {
return data;
}
}
答案 2 :(得分:0)
在您的Retrofit调用方法中 使用此
Call<Data> call = service.getMovie();
call.enqueue(new Callback<Data>() {
@Override
public void onResponse(Call<Data> call, Response<Data> response) {
if (response.isSuccessful()) {
Toast.makeText(MainActivity.this, "" + response.body().toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Data> call, Throwable t) {
Toast.makeText(MainActivity.this, "" + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
而不是
Call<List<Movie>> call = service.getMovie();
call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
if (response.isSuccessful()) {
Toast.makeText(MainActivity.this, "" + response.body().toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {
Toast.makeText(MainActivity.this, "" + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
您没有使用响应的基类,它应该是Data
,但您使用的是List<Movie>