Java android截取:全局变量不随方法第一次执行时的响应而变化。

Java android截取:全局变量不随方法第一次执行时的响应而变化。,java,android,android-layout,android-studio,android-volley,Java,Android,Android Layout,Android Studio,Android Volley,我有一个方法,在这个方法中,我发出一个截击请求,根据响应,我需要更改一个全局布尔变量,但由于某种原因,只有在该方法完全执行后,该变量才会更改,给我错误的变量数据。只有在收到响应后,我才需要以某种方式更改变量。。请帮帮我 我需要在响应时更改变量'chk'的值,但它不会更改 public boolean checkSourceCode() { pDialog.setMessage("Please Wait ..."); pDialog.show();

我有一个方法,在这个方法中,我发出一个截击请求,根据响应,我需要更改一个全局布尔变量,但由于某种原因,只有在该方法完全执行后,该变量才会更改,给我错误的变量数据。只有在收到响应后,我才需要以某种方式更改变量。。请帮帮我

我需要在响应时更改变量'chk'的值,但它不会更改

  public boolean checkSourceCode() {

        pDialog.setMessage("Please Wait ...");
        pDialog.show();

        final String entered_source_code = source_code.getText().toString();
        if(entered_source_code!=null || !entered_source_code.isEmpty()) {
            testString = entered_source_code;

            final StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String web_response) {
                    try {
                        response = new JSONObject(web_response);
                        Log.e("Resp SUCCESS", "" + response);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try {
                        hasBeenValidated = true;
                        if (response.getBoolean("success")) {
                            Log.e("Resp SUCCESS", "" + response);
                            validateCode = true;
                            chk=true; // THIS VALUE DOES NOT CHANGE ON FIRST CALL OF THE METHOD HOWEVER ON SECOND TIME CALLING THE METHOD IT CHANGED
                            //  Utils.reference_id = source_code.getText().toString().trim();
                            pDialog.hide();
                        input_layout_source_code.setError(null);
                        input_layout_source_code.setErrorEnabled(false);
                        Utils.reference_id = source_code.getText().toString().trim();
                        source_code.setBackground(source_code.getBackground().getConstantState().newDrawable());
                        } else {
                            validateCode = false;
                            chk=false;// THIS VALUE DOES NOT CHANGE ON FIRST CALL OF THE METHOD HOWEVER ON SECOND TIME CALLING THE METHOD IT CHANGED
                            pDialog.hide();
                        input_layout_source_code.setErrorEnabled(true);
                        input_layout_source_code.setError("Invalid reference Id.");
                        Utils.reference_id = null;
                        Toast.makeText(getContext(), "Invalid reference Id", Toast.LENGTH_SHORT).show();
                        }
                    //    chk = validateSourceCode();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    String json = null;

                    if (error instanceof NoConnectionError) {
                        String strerror = "No internet Access, Check your internet connection.";
                        displayMessage(strerror);
                    }
                    NetworkResponse response = error.networkResponse;
                    if (null != response && response.statusCode != 200) {
                        Log.e("Resp code", "" + response.statusCode);
                        displayMessage("Please contact administrator for error code " + response.statusCode);
                    }
                    if (response != null && response.data != null) {
                        switch (response.statusCode) {
                            case 400:
                                json = new String(response.data);
                                json = trimMessage(json, "message");
                                if (json != null) displayMessage(json);
                                break;
                            default:
                                json = new String(response.data);
                                json = trimMessage(json, "message");
                                if (json != null) displayMessage(json);
                        }
                    }
                }
            }
            ) {
                @Override
                public Request.Priority getPriority() {
                    return Priority.IMMEDIATE;
                }

                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> requestParams = new HashMap<String, String>();
                    requestParams.put("referral_code", entered_source_code);
//                params.put("email", "abc@androidhive.info");
//                params.put("password", "password123");

                    return requestParams;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("Auth-Token", auth_token);
                    return params;
                }
            };
            sr.setRetryPolicy(new DefaultRetryPolicy(
                    60000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            // Adding request to request queue
            MaintainRequestQueue.getInstance(mContext).addToRequestQueue(sr, "tag");
        }
        else{
            pDialog.hide();
            chk=true;// THIS VALUE DOES NOT CHANGE ON FIRST CALL OF THE METHOD HOWEVER ON SECOND TIME CALLING THE METHOD IT CHANGED
        }
        Toast.makeText(mContext, String.valueOf(chk), Toast.LENGTH_LONG).show();
        return chk;
    }
public boolean checkSourceCode(){
setMessage(“请稍候…”);
pDialog.show();
输入的最后一个字符串\u source\u code=source\u code.getText().toString();
if(输入的源代码!=null | |!输入的源代码.isEmpty()){
testString=输入的源代码;
final StringRequest sr=new StringRequest(Request.Method.POST,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串web\u响应){
试一试{
响应=新的JSONObject(web\u响应);
Log.e(“响应成功”,“响应+响应”);
}捕获(JSONException e){
e、 printStackTrace();
}
试一试{
hasbeenvalidate=真;
if(response.getBoolean(“success”)){
Log.e(“响应成功”,“响应+响应”);
validateCode=true;
chk=true;//此值在第一次调用该方法时不会更改,但在第二次调用它更改的方法时会更改
//Utils.reference_id=source_code.getText().toString().trim();
pDialog.hide();
输入布局源代码。设置错误(null);
输入布局源代码。setErrorEnabled(false);
Utils.reference_id=source_code.getText().toString().trim();
source_code.setBackground(source_code.getBackground().getConstantState().newDrawable());
}否则{
validateCode=false;
chk=false;//此值在第一次调用该方法时不会更改,但在第二次调用它更改的方法时会更改
pDialog.hide();
输入布局源代码。setErrorEnabled(真);
输入布局源代码setError(“无效引用Id”);
Utils.reference_id=null;
Toast.makeText(getContext(),“无效引用Id”,Toast.LENGTH_SHORT.show();
}
//chk=validateSourceCode();
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
字符串json=null;
if(错误实例of NoConnectionError){
String strerror=“无法访问internet,请检查您的internet连接。”;
显示消息(strerror);
}
NetworkResponse=错误。NetworkResponse;
如果(null!=响应和响应.statusCode!=200){
Log.e(“响应代码”,“响应+状态代码”);
显示消息(“请与管理员联系以获取错误代码”+响应。状态代码);
}
if(response!=null&&response.data!=null){
开关(响应状态代码){
案例400:
json=新字符串(response.data);
json=trimMessage(json,message);
如果(json!=null)显示消息(json);
打破
违约:
json=新字符串(response.data);
json=trimMessage(json,message);
如果(json!=null)显示消息(json);
}
}
}
}
) {
@凌驾
公共请求。Priority getPriority(){
返回优先级。立即返回;
}
@凌驾
受保护的映射getParams(){
Map requestParams=new HashMap();
requestParams.put(“参考代码”,输入源代码);
//参数put(“电子邮件”)abc@androidhive.info");
//参数put(“密码”、“密码123”);
返回请求参数;
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“身份验证令牌”,身份验证令牌);
返回参数;
}
};
高级setRetryPolicy(新的DefaultRetryPolicy)(
60000,
DefaultRetryPolicy.DEFAULT\u最大重试次数,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//将请求添加到请求队列
MaintainRequestQueue.getInstance(mContext).addToRequestQueue(sr,“标记”);
}
否则{
pDialog.hide();
chk=true;//此值在第一次调用该方法时不会更改,但在第二次调用它更改的方法时会更改
}
Toast.makeText(mContext,String.valueOf(chk),Toast.LENGTH_LONG.show();
返回chk;
}

chk变量无法在内部重新初始化