在post函数之外不存在变量

时间:2018-05-26 19:38:27

标签: java android

我将如何制作它以便我可以在tag_found函数之外引用我的变量TrakkRestClient.post,以便我可以检查它和user_found是否都为真,然后运行基于的代码是什么?

submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RequestParams tag_params = new RequestParams("Tag_ID", tag_id.getText().toString());
            TrakkRestClient.post("check/item", tag_params, new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] header, JSONObject response){
                    if (response.toString().contains("No")){
                        TrakkRestClient.post("log/add/in", tag_params, new JsonHttpResponseHandler());
                        Boolean tag_found = false;
                    }else if (response.toString().contains("found")){
                        Boolean tag_found = true;

                    }else{
                        show_err_tag();
                    }
                    Log.d("log",response.toString());
                }
            });
            RequestParams user_params = new RequestParams("email", user_email.getText().toString());
            TrakkRestClient.post("check/user", user_params, new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] header, JSONObject response){
                    if (response.toString().contains("found")){
                        final Boolean user_found = true;
                    }
                    else{
                        show_err_user();
                    }
                    Log.d("log",response.toString());
                }
            });
            Log.d("tag",tag_found.toString());

1 个答案:

答案 0 :(得分:0)

将其设为全局变量,可随时随地访问

submit.setOnClickListener(new View.OnClickListener() {
    Boolean tag_found = false;

    @Override
    public void onClick(View v) {
        RequestParams tag_params = new RequestParams("Tag_ID", tag_id.getText().toString());
        TrakkRestClient.post("check/item", tag_params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] header, JSONObject response){
                if (response.toString().contains("No")){
                    TrakkRestClient.post("log/add/in", tag_params, new JsonHttpResponseHandler());
                   tag_found = false;
                }else if (response.toString().contains("found")){
                   tag_found = true;

                }else{
                    show_err_tag();
                }
                Log.d("log",response.toString());
            }
        });
        RequestParams user_params = new RequestParams("email", user_email.getText().toString());
        TrakkRestClient.post("check/user", user_params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] header, JSONObject response){
                if (response.toString().contains("found")){
                    final Boolean user_found = true;
                }
                else{
                    show_err_user();
                }
                Log.d("log",response.toString());
            }
        });
        Log.d("tag",tag_found.toString());