简体   繁体   English

获取响应404和应用崩溃的改进

[英]getting response 404 and app crash retrofit

I am trying to implement retrofit for get and store advertise related int value in my android application. 我试图在我的android应用程序中实现获取和存储广告相关int值的改造。 I am getting response like {"banner_on":"1","int_on":"1","int_click":"3"} from API. 我从API收到类似{"banner_on":"1","int_on":"1","int_click":"3"}

I am using code in splash screen like this 我在这样的启动画面中使用代码

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) {

                 }
            });

My Ads Model is like below 我的广告模型如下所示

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;
    }
}

and my ApiService is like below 我的ApiService如下所示

 @GET("getads.php")
    Call<Ads>getads();

I am getting error like below 我收到如下错误

java.lang.NullPointerException: Attempt to invoke virtual method 'int com.myapp.example.model.Ads.banner_on()' on a null object reference

Let me know if someone can correct me where I am wrong. 让我知道是否有人可以纠正我的错误之处。 Thanks 谢谢

The response.body() will be null if the request is unsuccessful. 如果请求失败,则response.body()将为null。

if(response.isSuccessful()) //use response.body();
else // use response.errorBody();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM