Android:排球请求永远不会完成

时间:2018-06-17 05:57:53

标签: android android-volley

在我的Android应用程序中,当我开始使用之前保存的令牌跳过登录页面时,我正在尝试。由于令牌最终到期,我尝试做的是在用户尝试登录之前我想将之前保存的令牌发送到服务器,以便服务器可以检查它是否仍然可用。为此,我使用Volley发出请求,但请求永远不会完成。它不会在调试器中出现任何错误,也不会触及OnRespone或OnErrorResponse上的任何断点。

我已经检查过请求是否到达服务器并且确实如此,服务器返回我想要的响应没有问题。这是我的代码:

排球功能:

private void validateTokenLifeTime()
    {
        String tag_json_obj = "json_obj_req";
        String uri = String.format(URL + "/userdata/ValidateTokenLife");

        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Checking if logged in...");
        pDialog.show();

        StringRequest jsonObjReq = new StringRequest(Request.Method.GET, uri, new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response)
            {
                if (response.compareTo("true") != 0)
                {
                    pDialog.dismiss();
                    Intent intent = new Intent(getBaseContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                }

            }

        }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                pDialog.dismiss();
                Log.e("ERROR", "Error occurred ", error);
                Snackbar snackbar2 = Snackbar.make(getWindow().getDecorView().getRootView(), "Login expired. Please login again", Snackbar.LENGTH_SHORT).setDuration(2000);
                snackbar2.show();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() {
                SharedPreferences settings = PreferenceManager
                        .getDefaultSharedPreferences(LoginActivity.this);
                String auth_token_string = settings.getString("token", "");

                Map<String, String> params = new HashMap<>();
                params.put("Content-Type", "application/json; charset=UTF-8");
                params.put("Authorization", "Basic " + auth_token_string);
                return params;
            }
        };

        jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
                3000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    }

onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
    String auth_token = settings.getString("token", "");

    email = findViewById(R.id.emailInput);
    password = findViewById(R.id.passwordInput);
    Button loginButton = findViewById(R.id.loginButton);
    Button registerButton = findViewById(R.id.registerBut);

    if (CheckNetwork.isNetworkConnected(this))
    {
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                validate(email.getText().toString(), password.getText().toString());
            }
        });
    }
    else if(auth_token.equalsIgnoreCase(""))
    {
        AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create();
        alertDialog.setTitle("Warning");
        alertDialog.setMessage("No internet connection. You won't be able to login");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
        registerButton.setEnabled(false);
        loginButton.setEnabled(false);
    }
    registerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getBaseContext(), RegisterActivity.class);
            startActivity(intent);
        }
    });

    if (!auth_token.equals(""))
    {
        validateTokenLifeTime();
    }
}

我试图在OnCreate()中调用我的Volley函数的事实可以成为问题吗?如果是,我如何在不按任何东西的情况下打开应用程序时立即检查令牌服务器端?谢谢。

0 个答案:

没有答案