android volley:全局变量不会改变,具体取决于第一次执行方法时的响应。

时间:2016-11-01 06:28:01

标签: java android android-layout android-studio android-volley

我有一个方法,我正在进行一个排球请求,并根据我需要更改一个全局布尔变量的响应,但由于某种原因,变量只有在执行完全给我错误的变量数据后才会被更改。我需要以某种方式更改变量,只有在收到响应后才能更改..请帮助我

我需要更改变量' 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;
    }

2 个答案:

答案 0 :(得分:0)

chk变量无法在线程内再次重新初始化 试试这个

setchk(boolean chk)

{ this.chk = CHK;

}

并从请求方法中调用它;

答案 1 :(得分:0)

您到达的排球请求中的变量在您到达时未初始化:Toast.makeText(mContext,String.valueOf(chk),Toast.LENGTH_LONG)。show();使用get方法获取齐射中的chk值,并在需要的地方调用该getter。