Android截击从登录php获得响应,但不进入下一个活动

Android截击从登录php获得响应,但不进入下一个活动,php,android,mysql,Php,Android,Mysql,Android登录问题。Php以JSON格式完美地返回响应。但android活动不会进入下一个活动。有时显示网络连接不良,有时显示密码不匹配错误,即使密码正确 这是我的密码 login.php <?php include("Connection.php"); if(isset($_POST["email"]) && isset($_POST["password"])) { $email=$_POST["email"]; $password=$_POS

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

这是我的密码

login.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);
}


?>

您正在将JSON结果与一个错误的数字进行比较

试试这个

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

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

if..else
条件上方将此块作为

$isLogin["success"] = 0; //This will give access outside of If condition.
if(mysqli_num_rows($result) > 0)
{   
    $isLogin["success"] = 1;

}           

echo json_encode($isLogin);
正如@Lucifer发布的,服务器正在返回
{“success”:“1”}
,在您的
活动中编写您的as

JSONObject jsonObject = new JSONObject(response);
if(jsonObject.getString("success").equals("1"))
{
     //Success
}
else{
    //Invalid Login
}

请尝试此代码,它对您非常有帮助:

    String url = Global.BASE_URL + Global.LOGIN_API;
    cancel_login_api=url;
    StringRequest jsonObjReq = new StringRequest(Request.Method.POST,
            url,
            new Response.Listener<String>() {
                /** Api response action*/
                @Override
                public void onResponse(String response) {
                      Log.e("AB",response);
                     /** receiving response from api here*/
                    try {
                        JSONObject obj = new JSONObject(response);
                        if (API.success(obj))
                        {

                            Utils.storeUserPreferences(LoginActivity.this, Global.USER, API.getData(obj));
                            Toast.makeText(LoginActivity.this, API.rMessage(obj), Toast.LENGTH_SHORT).show();
                            Intent skip = new Intent(LoginActivity.this, HomeActivity.class);
                            startActivity(skip);
                        } else
                        {
                            Toast.makeText(LoginActivity.this, ""+API.rMessage(obj), Toast.LENGTH_SHORT).show();
                        }
                    }   catch (Exception e) {
                        e.printStackTrace();
                        Log.e("AA",response);
                    }

                    /** Alert dialog box is dismiss*/
                    pd.dismiss();
                }
            }, new Response.ErrorListener() {
        /** method when no response from internet*/
        @Override
        public void onErrorResponse(VolleyError error) {
            /** Error Log */
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Log.e("AA",""+error.getMessage());

            /** Alert dialog box is dismiss*/
            pd.dismiss();
        }
      }) {

        /*
        ** Sending parameters to server
        * @params user_login
        * @params user_login_password
        */

        @Override
        protected Map<String, String> getParams() {

          /** Pass the parameters to according to the API.*/
            Map<String, String> params = new HashMap<String, String>();
            params.put("user_login", edt_email.getText().toString().trim());
            params.put("user_login_password", edt_password.getText().toString().trim());
            return params;
        }
    };
    /** Adding request to request queue*/
    AppController.getInstance().addToRequestQueue(jsonObjReq, cancel_login_api);
}
String url=Global.BASE\u url+Global.LOGIN\u API;
取消\u登录\u api=url;
StringRequest JSONOBJEQ=新的StringRequest(Request.Method.POST,
网址,
新的Response.Listener(){
/**Api响应动作*/
@凌驾
公共void onResponse(字符串响应){
Log.e(“AB”,响应);
/**这里接收来自api的响应*/
试一试{
JSONObject obj=新的JSONObject(响应);
如果(API.success(obj))
{
Utils.storeUserPreferences(LoginActivity.this、Global.USER、API.getData(obj));
Toast.makeText(LoginActivity.this、API.rMessage(obj)、Toast.LENGTH_SHORT.show();
意图跳过=新意图(LoginActivity.this、HomeActivity.class);
星触觉(跳跃);
}否则
{
Toast.makeText(LoginActivity.this,“+API.rMessage(obj),Toast.LENGTH_SHORT.show();
}
}捕获(例外e){
e、 printStackTrace();
Log.e(“AA”,答复);
}
/**“警报”对话框已关闭*/
pd.解散();
}
},new Response.ErrorListener(){
/**当没有来自internet的响应时的方法*/
@凌驾
公共无效onErrorResponse(截击错误){
/**错误日志*/
d(标记“Error:+Error.getMessage());
Log.e(“AA”,“error.getMessage());
/**“警报”对话框已关闭*/
pd.解散();
}
}) {
/*
**向服务器发送参数
*@params用户登录
*@params用户\登录\密码
*/
@凌驾
受保护的映射getParams(){
/**根据API将参数传递到*/
Map params=新的HashMap();
参数put(“用户登录”,edt_email.getText().toString().trim());
参数put(“用户登录密码”,edt密码.getText().toString().trim());
返回参数;
}
};
/**将请求添加到请求队列*/
AppController.getInstance().addToRequestQueue(JSONOBJEQ,取消登录\u api);
}

试试这个。

你也可以试试if(jsonObject.getString(“success”).equalsIgnoreCase(“1”))谢谢它奏效了,但我也必须在php中进行更改。
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.getString("success").equals("1"))
{
     //Success
}
else{
    //Invalid Login
}
    String url = Global.BASE_URL + Global.LOGIN_API;
    cancel_login_api=url;
    StringRequest jsonObjReq = new StringRequest(Request.Method.POST,
            url,
            new Response.Listener<String>() {
                /** Api response action*/
                @Override
                public void onResponse(String response) {
                      Log.e("AB",response);
                     /** receiving response from api here*/
                    try {
                        JSONObject obj = new JSONObject(response);
                        if (API.success(obj))
                        {

                            Utils.storeUserPreferences(LoginActivity.this, Global.USER, API.getData(obj));
                            Toast.makeText(LoginActivity.this, API.rMessage(obj), Toast.LENGTH_SHORT).show();
                            Intent skip = new Intent(LoginActivity.this, HomeActivity.class);
                            startActivity(skip);
                        } else
                        {
                            Toast.makeText(LoginActivity.this, ""+API.rMessage(obj), Toast.LENGTH_SHORT).show();
                        }
                    }   catch (Exception e) {
                        e.printStackTrace();
                        Log.e("AA",response);
                    }

                    /** Alert dialog box is dismiss*/
                    pd.dismiss();
                }
            }, new Response.ErrorListener() {
        /** method when no response from internet*/
        @Override
        public void onErrorResponse(VolleyError error) {
            /** Error Log */
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Log.e("AA",""+error.getMessage());

            /** Alert dialog box is dismiss*/
            pd.dismiss();
        }
      }) {

        /*
        ** Sending parameters to server
        * @params user_login
        * @params user_login_password
        */

        @Override
        protected Map<String, String> getParams() {

          /** Pass the parameters to according to the API.*/
            Map<String, String> params = new HashMap<String, String>();
            params.put("user_login", edt_email.getText().toString().trim());
            params.put("user_login_password", edt_password.getText().toString().trim());
            return params;
        }
    };
    /** Adding request to request queue*/
    AppController.getInstance().addToRequestQueue(jsonObjReq, cancel_login_api);
}