[英]getting response 404 and app crash retrofit
我试图在我的android应用程序中实现获取和存储广告相关int值的改造。 我从API收到类似{"banner_on":"1","int_on":"1","int_click":"3"}
。
我在这样的启动画面中使用代码
Retrofit retrofit = ApiClient.getClient();
ApiService apiService = retrofit.create(ApiService.class);
apiService.getads().enqueue(new Callback<Ads>() {
@Override
public void onResponse(Call<Ads> call, Response<Ads> response) {
constant.banner_on = response.body().banner_on();
constant.int_on = response.body().int_on();
constant.int_click = response.body().int_click();
}
@Override
public void onFailure(Call<Ads> call, Throwable t) {
}
});
我的广告模型如下所示
public final class Ads {
@SerializedName("success")
private int banner_on;
private int int_on;
private int int_click;
public int banner_on() {
return banner_on;
}
public int int_on() {
return int_on;
}
public int int_click() {
return int_click;
}
}
我的ApiService如下所示
@GET("getads.php")
Call<Ads>getads();
我收到如下错误
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.myapp.example.model.Ads.banner_on()' on a null object reference
让我知道是否有人可以纠正我的错误之处。 谢谢
如果请求失败,则response.body()将为null。
if(response.isSuccessful()) //use response.body();
else // use response.errorBody();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.