1

I am trying to implement login activity and return the user ID value to use it in other activity ,I have written this php script to login:

<?php
$DB_USER='user'; 
$DB_PASS='pass'; 
$DB_HOST='localhost';
$DB_NAME='db';
$username = $_POST['username'];
$password = $_POST['password'];

$con=mysqli_connect($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
$sql=mysqli_prepare($con,"SELECT * FROM tABLE WHERE  UserName=? AND Password=?");
mysqli_stmt_bind_param($sql,"ss",$username,$password);
mysqli_stmt_execute($sql);
mysqli_stmt_store_result($sql);
mysqli_stmt_bind_result($sql,$id,$username,$password);
$response=array();
$response["sucess"]=false;
while(mysqli_stmt_fetch($sql))
{
    $response["sucess"]=true;
    $response["ID"]=$id;        //   $result="true"; 
}  


// send result back to android
echo json_encode($response);
?>

and in the code inside the login activity I used Volley library and I have written this script

   final String username = etUsername.getText().toString();
        final String password = etPassword.getText().toString();
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                //  Toast.makeText(Login.this,response,Toast.LENGTH_LONG).show();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    boolean responsestatus = jsonObject.getBoolean("sucess");
                    Log.d("ADebugTag", "Value: " + responsestatus);

                    if (responsestatus) {

                        String Id = jsonObject.getString("ID");
                        Intent intent = new Intent(Login.this, pilgrimdashboard.class);
                        intent.putExtra("ID", Id);
                        startActivity(intent);
                        Login.this.finish();

                    } else {

    Toast.makeText(Login.this, "Wrong!!!", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show();

            }
        }
        ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();

                params.put(kusername, username);
                params.put(kpassword, password);

                return params;
            }
        };

        requestQueue.add(stringRequest);

The problem I got is when I click on login button nothing is happening which frustrate me a bit because I could not recognize the source of the problem , Thanks in advance

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

0 Answers0