php - Android Volley 从登录 php 获取响应但不进入下一个 Activity

标签 php android mysql

Android 登录问题。 Php 完美地以 JSON 格式返回响应。但 Android Activity 不会进入下一个 Activity 。有时显示网络连接不良,有时显示密码不匹配错误,即使密码正确

这是我的代码

登录.php

  <?php

include("Connection.php");

if(isset($_POST["email"]) && isset($_POST["password"]))
{

   $email=$_POST["email"];

   $password=$_POST["password"];

   $result = mysqli_query($conn, "select * from user_master where email='$email' && password='$password'");

    if(mysqli_num_rows($result) > 0)
    {   
        $isLogin["success"] = 1;

    }           
    else
    {   
        $isLogin["success"] = 0;
    }
    echo json_encode($isLogin);
}


?>

LoginRequest.java

    package com.talentakeaways.ttpms;

import com.android.volley.AuthFailureError;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;



import java.util.HashMap;
import java.util.Map;

/**
 * Created by chand on 15-03-2018.
 */

public class LoginRequest extends StringRequest {


    private static final String LOGIN_URL = "http://192.168.1.9:80/Ttpms/login.php";
    private Map<String, String> parameters;
    LoginRequest(String username, String password, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(Method.POST, LOGIN_URL, listener, errorListener);
        parameters = new HashMap<>();
        parameters.put("email", username);
        parameters.put("password", password);
    }
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return parameters;
    }
}

Ttpm_Login.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ttpm_login);
        setTitle("Login"); //set title of the activity
        initialize();
        final RequestQueue requestQueue = Volley.newRequestQueue(Ttpm_Login.this);
        //onClickListener method for button
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //assigning String variables to the text in edit texts
                userName = tenantname.getText().toString();
                password = passWord.getText().toString();
                //Validating the String values
                if (validateUsername(userName) && validatePassword(password)) {

                    //Start ProgressDialog
                    final ProgressDialog progressDialog = new ProgressDialog(Ttpm_Login.this);
                    progressDialog.setTitle("Please Wait");
                    progressDialog.setMessage("Logging You In");
                    progressDialog.setCancelable(false);
                    progressDialog.show();


                    //LoginRequest from class LoginRequest
                    LoginRequest loginRequest = new LoginRequest(userName, password, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.i("Login Response", response);
                            progressDialog.dismiss();
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                //If Success then start Dashboard Activity
                                if (jsonObject.getString("success").equals(1)) {
                                    Intent loginSuccess = new Intent(getApplicationContext(), Ttpm_Dashboard.class);
                                    startActivity(loginSuccess);
                                    finish();
                                }

                                //else Invalid
                                else {
                                    if (jsonObject.getString("success").equals(0))
                                        Toast.makeText(getApplicationContext(), "User Not Found", Toast.LENGTH_SHORT).show();
//                                    else {
//                                        Toast.makeText(getApplicationContext(), "Passwords Don't Match", Toast.LENGTH_SHORT).show();
//                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                                Log.getStackTraceString(e);
                                Toast.makeText(Ttpm_Login.this, "Bad Response from the Server", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            progressDialog.dismiss();
                            if (error instanceof ServerError) {
                                Toast.makeText(Ttpm_Login.this, "Server Error", Toast.LENGTH_SHORT).show();
                            } else if (error instanceof TimeoutError) {
                                Toast.makeText(Ttpm_Login.this, "Connection Timed Out", Toast.LENGTH_SHORT).show();
                            } else if (error instanceof NetworkError) {
                                Toast.makeText(Ttpm_Login.this, "Bad Network Connection", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    requestQueue.add(loginRequest);
                }
            }
        });
    }

请帮忙提前致谢。

最佳答案

您将 JSON 结果与错误的数字进行比较

试试这个

   if (jsonObject.getString("success").equals("1"))  {
      // Proceed to Login
   }  else { 
          if (jsonObject.getString("success").equals("0")) {
   }

服务器正在返回{ "success": "1"}

关于php - Android Volley 从登录 php 获取响应但不进入下一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49421584/

相关文章:

php - 使用 PHP 在页面的标题标签内运行 SQL 查询。安全练习?

php - Paypal 沙盒 : DoExpressCheckoutPayment 10001 Internal Error

php - 用于查找 HTML 字符串中所有路径的正则表达式

java - 如何将文件写入 Android SD 卡并在 PC 上可见

mysql - 在 MySQL 中创建 Single Blob 列来保存 BSON 是否符合 Mongo DB 数据库的目的?

php - Datetimepicker时间日期转换

javascript - 将 PHP 数组发送到 JavaScript 问题

android - 无法在android上编译avahi

android - 如何在重新启动应用程序时显示进度屏幕?

PHPUnit/DBUnit - createDefaultDBConnection() 不接受架构名称