jsonRequest被延迟

时间:2018-07-31 09:15:54

标签: android android-volley android-json

我有一个用于登录的jsonRequest函数。无论查询是否成功,该函数都会更改一个布尔值(命名标志)。我在Button OnClickListener内的tryLogin函数上调用该函数。

似乎第一次没有调用jsonRequest函数(布尔值未更改)。但是在第二次尝试中,使用相同的输入,布尔值将被更改。后来我发现在布尔检查之后调用了jsonRequest函数。

有人知道如何解决此问题吗?我将在下面发布代码。谢谢:)

jsonRequest函数(无法发布url,但可以使用):

public void jsonRequest()  {

    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                           // response
                     Log.d("Response", response);
                    if (response.equals("success")) {
                        flag = true;
                        Log.d ("flag2", String.valueOf(flag));
                    }
                    else {
                        flag = false;
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", String.valueOf(error));
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("email", mEmailView.getText().toString());
            params.put("pass", mPasswordView.getText().toString());

            return params;
        }
    };
    queue.add(postRequest);
}

这是我称为jsonRequest和布尔检查的地方。

if( ! password.isEmpty() && ! email.isEmpty()) {

        jsonRequest();


        Log.e("FLAG", String.valueOf(flag));

        if (flag == true) {
            cancel = false;
        }
        else {
            cancel = true;
            mPasswordView.setText("");
            focusView = mEmailView;
            Toast.makeText(getApplicationContext(), "Invalid email or password", Toast.LENGTH_SHORT).show();
        }

2 个答案:

答案 0 :(得分:0)

您的if condition始终为false,因为您没有更新分配给它的默认值,即flagfalse

尝试一下

if( !( password.isEmpty() &&  email.isEmpty())) {

            jsonRequest();

            Log.e("FLAG", String.valueOf(flag));

            cancel = false;
            }
            else {
            cancel = true;
           // mPasswordView.setText("");
            focusView = mEmailView;
            Toast.makeText(getApplicationContext(), "Please enter email and password", Toast.LENGTH_SHORT).show();
            }
            }

和jsonRequest();

public void jsonRequest()  {

    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                           // response
                     Log.d("Response", response);
                    if (response.equals("success")) {
                        flag = true;
                        Log.d ("flag2", String.valueOf(flag));
                    }
                    else {
                        flag = false;
Toast.makeText(getApplicationContext(), "Invalid email or password", Toast.LENGTH_SHORT).show();
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", String.valueOf(error));
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("email", mEmailView.getText().toString());
            params.put("pass", mPasswordView.getText().toString());

            return params;
        }
    };
    queue.add(postRequest);
}

答案 1 :(得分:0)

尝试以下代码,通过截击发出的每个请求都是异步的,onResponse和onError回调在UI线程上执行,因此您的代码在此处得到纠正。

 public void jsonRequest()  {

  StringRequest postRequest = new StringRequest(Request.Method.POST, url,
        new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response) {
                       // response
                 Log.d("Response", response);
                if (response.equals("success")) {
                    flag = true;
                    Log.d ("flag2", String.valueOf(flag));
                    cancel = false;      

                }
                else {
                    flag = false;
                    cancel = true;
                    mPasswordView.setText("");  
                    focusView = mEmailView;   
                    Toast.makeText(getApplicationContext(), "Invalid email or password", Toast.LENGTH_SHORT).show();         
                }
            }
        },
        new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error) {
                // error
                Log.d("Error.Response", String.valueOf(error));
                //TODO: Please handle error scenario, which is missing.    
            }
        }
  ) {
    @Override
    protected Map<String, String> getParams()
    {
        Map<String, String>  params = new HashMap<String, String>();
        params.put("email", mEmailView.getText().toString());
        params.put("pass", mPasswordView.getText().toString());

        return params;
    }
};
queue.add(postRequest); 
}