Android / PHP:登录成功但失败重定向到下一个活动

Posted

技术标签:

【中文标题】Android / PHP:登录成功但失败重定向到下一个活动【英文标题】:Android / PHP: Login successful but Failed redirect to the next activity 【发布时间】:2020-05-28 20:47:57 【问题描述】:

我是 android 编程新手。目前,我开发需要登录的应用程序。目前,登录成功。下面是代码:

MainActivity.JAVA

private void userLogin() 
    //first getting the values
    final String badgeid = etBadgeid.getText().toString();
    final String pwd = etPassword.getText().toString();

    //validating inputs
    if (TextUtils.isEmpty(badgeid)) 
        etBadgeid.setError("Please enter your badgeid");
        etBadgeid.requestFocus();
        return;
    

    if (TextUtils.isEmpty(pwd)) 
        etPassword.setError("Please enter your password");
        etPassword.requestFocus();
        return;
    

    //if everything is fine
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_LOGIN,
            new Response.Listener<String>() 
                @Override
                public void onResponse(String response) 

                    Log.i("onPostExecute","response is: "+response);

                    try 
                        //converting response to json object
                        JSONObject obj = new JSONObject(response);

                        //if no error in response
                        if (!obj.getBoolean("error")) 
                            Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                            //getting the user from the response
                            JSONObject userJson = obj.getJSONObject("user");

                            //creating a new user object
                            User user = new User(
                                    userJson.getString("badgeid"),
                                    userJson.getString("email"),
                                    userJson.getString("fullname"),
                                    userJson.getInt("roles_id"),
                                    userJson.getInt("team_id")
                            );

                            //storing the user in shared preferences
                            SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);

                            //starting the profile activity
                            finish();
                            startActivity(new Intent(getApplicationContext(), Home.class));
                         else 
                            Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                        
                     catch (JSONException e) 
                        e.printStackTrace();
                    
                
            ,
            new Response.ErrorListener() 
                @Override
                public void onErrorResponse(VolleyError error) 
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                
            ) 
        @Override
        protected Map<String, String> getParams() 
            Map<String, String> params = new HashMap<>();
            params.put("badgeid", badgeid);
            params.put("pwd", pwd);
            return params;
        
    ;

    RequestQueue requestQueue= Volley.newRequestQueue(getApplicationContext());
    requestQueue.add(stringRequest);

URL_LOGIN (php)

<?php 

    require_once '../config/configPDO.php';

    $response = array();

        if(isTheseParametersAvailable(array('badgeid', 'pwd')))

            $badgeid = $_POST['badgeid'];
            $pwd = $_POST['pwd'];

            $stmt = $conn->prepare("SELECT badgeid, email, fullname, roles_id, team_id FROM ot_users WHERE badgeid = :badgeid AND pwd = :pwd AND roles_id = 7 AND team_id <> 1");

            $stmt->bindParam(':badgeid',$badgeid,PDO::PARAM_STR);
            $stmt->bindParam(':pwd',$pwd,PDO::PARAM_STR);
            $stmt->execute();
            $result = $stmt->fetch(PDO::FETCH_ASSOC);

            if ($result) 

                $response['error'] = false; 
                $response['message'] = 'Login successfull'; 
                $response['user'] = $result;  

            else
                $response['error'] = false; 
                $response['message'] = 'Invalid username or password';
            
        

    echo json_encode($response);

    function isTheseParametersAvailable($params)

        foreach($params as $param)
            if(!isset($_POST[$param]))
                return false; 
            
        
        return true; 
    

?>

然后,我想在从数据库中检索数据时使用 Web 服务 (url)。下面是php代码

URL_LOGIN (PHP)

<?php 

    require_once '../config/configPDO.php';

    $response = array();

        if(isTheseParametersAvailable(array('badgeid', 'pwd')))

            $badgeid = $_POST['badgeid'];
            $pwd = $_POST['pwd'];

            $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/ot_displayUser?badgeid=$badgeid&pwd=$pwd";
            $data = file_get_contents($url);
            $json = json_decode($data);
            $result = $json->otUserList;

            if ($result) 

                $response['error'] = false; 
                $response['message'] = 'Login successfull'; 
                $response['user'] = $result;  

            else
                $response['error'] = false; 
                $response['message'] = 'Invalid username or password';
            
        

    echo json_encode($response);

    function isTheseParametersAvailable($params)

        foreach($params as $param)
            if(!isset($_POST[$param]))
                return false; 
            
        
        return true; 
    

?>

当我使用此代码时,它会弹出“登录成功”但它无法重定向到下一页(Home.JAVA)。谁能知道是什么问题?

【问题讨论】:

我可以知道您在开始活动时遇到任何崩溃或其他问题吗? @DipankarBaghel 没有。只需弹出“登录成功”。但是如果我使用上面的第一个 PHP 代码,它也会弹出并重定向到下一个活动 【参考方案1】:

您在startActivity(new Intent(MainActivity.this, Home.class)); 之前调用finish() 方法。 finish() 用于从堆栈中删除当前活动。您总是必须在导航到另一个活动后调用。

如果您认为这是正确答案,请更正它。

已更新您的代码,因此请复制以下代码:

MainActivity.java

private void userLogin() 
    //first getting the values
    final String badgeid = etBadgeid.getText().toString();
    final String pwd = etPassword.getText().toString();

    //validating inputs
    if (TextUtils.isEmpty(badgeid)) 
        etBadgeid.setError("Please enter your badgeid");
        etBadgeid.requestFocus();
        return;
    

    if (TextUtils.isEmpty(pwd)) 
        etPassword.setError("Please enter your password");
        etPassword.requestFocus();
        return;
    

    //if everything is fine
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_LOGIN,
            new Response.Listener<String>() 
                @Override
                public void onResponse(String response) 

                    Log.i("onPostExecute","response is: "+response);

                    try 
                        //converting response to json object
                        JSONObject obj = new JSONObject(response);

                        //if no error in response
                        if (!obj.getBoolean("error")) 
                            Toast.makeText(MainActivity.this, obj.getString("message"), Toast.LENGTH_SHORT).show();

                            //getting the user from the response
                            JSONObject userJson = obj.getJSONObject("user");

                            //creating a new user object
                            User user = new User(
                                    userJson.getString("badgeid"),
                                    userJson.getString("email"),
                                    userJson.getString("fullname"),
                                    userJson.getInt("roles_id"),
                                    userJson.getInt("team_id")
                            );

                            //storing the user in shared preferences
                            SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);

                            //starting the profile activity

                            startActivity(new Intent(MainActivity.this, Home.class));
                            finish();  //it will remove current activity from activity stack
                         else 
                            Toast.makeText(MainActivity.this, obj.getString("message"), Toast.LENGTH_SHORT).show();
                        
                     catch (JSONException e) 
                        e.printStackTrace();
                    
                
            ,
            new Response.ErrorListener() 
                @Override
                public void onErrorResponse(VolleyError error) 
                    Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                
            ) 
        @Override
        protected Map<String, String> getParams() 
            Map<String, String> params = new HashMap<>();
            params.put("badgeid", badgeid);
            params.put("pwd", pwd);
            return params;
        
    ;

    RequestQueue requestQueue= Volley.newRequestQueue(MainActivity.this);
    requestQueue.add(stringRequest);

【讨论】:

这不是答案,兄弟。我昨天也收到了同样的答案。但他又删了。

以上是关于Android / PHP:登录成功但失败重定向到下一个活动的主要内容,如果未能解决你的问题,请参考以下文章

Grails Spring Security登录失败未重定向到登录视图

登录成功后重定向到新页面

登录后重定向到上一页? PHP

身份验证重定向失败

登录失败后设计重定向

成功登录重定向到登录页面