我想在Retrofit2的onResponse回调内连接/附加字符串

全局变量

Boolean isSaved = "false";
String message = "";


    Queries queries = Server.getClient().create(Queries.class);
    Call<ResponseModel> responseModelCall = queries.products(param1, param2);

    responseModelCall.enqueue(new Callback<ResponseModel>() {
        @Override
        public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
            if(response.isSuccessful()) {
                if(response.body().getCode().equals("1")) {
                    message = "Success";
                    isSaved = true;
                } else {
                    message = "Failed";
                    isSaved = false;
                }
            } else {
                    message = "Error";
            }
        }

        @Override
        public void onFailure(Call<ResponseModel> call, Throwable t) {

        }
    });

    if(isSaved){
        Toast.maketext(getActivity, "Congrats: "+message, LENGTH_SHORT).show();
    } else {
        Toast.maketext(getActivity, "I am sorry: "+message, LENGTH_SHORT).show();
    }


如果保存了数据,那么敬酒是
“恭喜: ”

如果不
“对不起: ”

我注意到我从onResponse传递的字符串不起作用。但是布尔值确实起作用。

最佳答案

如果要烤面包,应将其放在onResponse内。

responseModelCall.enqueue(new Callback<ResponseModel>() {
        @Override
        public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
            if(response.isSuccessful()) {
                if(response.body().getCode().equals("1")) {
                    message = "Success";
                    isSaved = true;
                } else {
                    message = "Failed";
                    isSaved = false;
                }
            } else {
                    message = "Error";
            }

            if (isSaved){
                Toast.maketext(getActivity, "Congrats: "+message, LENGTH_SHORT).show();
            } else {
                Toast.maketext(getActivity, "I am sorry: "+message, LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseModel> call, Throwable t) {

        }
    });

10-08 18:31