2

I am trying to set a register and login activity for an Android app with help of volley. i have been able to set up registration page up I have a problem with login activity
as email and password do not get matched with the database . i am using a online server there is a code to my login activity

        package com.gjs.tablepay;

 import android.app.ProgressDialog;
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


public class LoginActivity extends AppCompatActivity {

EditText etxtloginemail,etxtloginpassword;
TextView txtRegister,txtforgotpass;
Button btnlogin;
  PersonBean personbean;

// Is the Request to the Server
StringRequest stringRequest;

// Executes the Request
RequestQueue requestQueue;
ArrayList<PersonBean> personList;
ProgressDialog pd;

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

    etxtloginemail=(EditText)findViewById(R.id.editTextloginemail);
    etxtloginpassword=(EditText)findViewById(R.id.editTextloginpassword);
    btnlogin=(Button)findViewById(R.id.buttonLogin);
    txtRegister=(TextView)findViewById(R.id.textViewregister);
    txtforgotpass=(TextView)findViewById(R.id.textViewforgotpass);

    personbean = new PersonBean();

    // Initialize Volley's Request Queue
    requestQueue = Volley.newRequestQueue(this);
    pd = new ProgressDialog(this);

    txtRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this,RegistrationActivity.class));
        }
    });
    txtforgotpass.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                startActivity(new Intent(LoginActivity.this,ForgotpassActivity.class));
        }
    });
    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            init();
        }
    });}

void retrievePerson(){

    pd.show();
    stringRequest = new StringRequest(Request.Method.POST, Util.RETRIEVE_URL,

            // success
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    pd.dismiss();
                        Toast.makeText(LoginActivity.this,"yo",Toast.LENGTH_LONG).show();

                        if(s.equalsIgnoreCase("success")){
                            Toast.makeText(LoginActivity.this,"yo",Toast.LENGTH_LONG).show();
                            startActivity(new Intent(LoginActivity.this,HomeActivity.class));

                        }else{
                            pd.dismiss();
                            Toast.makeText(LoginActivity.this,"No Person Found",Toast.LENGTH_LONG).show();
                        }

                    }

            },

            // failure
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                   // Toast.makeText(AllPersonsActivity.this,"Some Volley Error", Toast.LENGTH_LONG).show();
                    Log.i("AllPersonsActivity", volleyError.toString());
                    Log.i("AllPersonsActivity",volleyError.getMessage());
                }
            }
    );

    // Execute the StringRequest
    requestQueue.add(stringRequest);

}
    void init(){
          personbean.setEmail(etxtloginemail.getText().toString().trim());
        personbean.setPassword(etxtloginpassword.getText().toString().trim());
        if(personbean.validateLoginPerson()){

            if(Util.isNetworkConnected(this)){

                retrievePerson();

            }else{
                Toast.makeText(this,"Please check your connectivity",Toast.LENGTH_LONG).show();
            }

        }else{
            Toast.makeText(this,"Please Enter Details First",Toast.LENGTH_LONG).show();
        }
    }

}

and here is my login.php

 <?php
$email=$_POST['email'];
$password=$_POST['password'];
include("dbconfig.php");

  $user = @mysql_query("select uid from users where email='$email' and password='$password'");  

 $row = mysql_fetch_array($user,MYSQL_ASSOC);
      $active = $row['active'];
$count = @mysql_num_rows($user);

$response =array();

 if($count==1){
    $response['success']=1;
    $response['message']="Records Retrieved sucessfully";
 }else{
    $response['success']=0;
    $response['message']="Retrieval Failure";
 }
 echo json_encode($response);
?>
Ianardo
  • 69
  • 12
Gsingh
  • 91
  • 9
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 14 '16 at 20:36
  • **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Nov 14 '16 at 20:36
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! ***SQL Injection!*** *It's not just for breakfast any more!* – Jay Blanchard Nov 14 '16 at 20:36
  • Have you checked your error logs? You're making an assumption the query is working. Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Nov 14 '16 at 20:36

1 Answers1

1

In your code you are missing getParams() where you specify your variables. It should look like this:

StringRequest request = new StringRequest(Request.Method.POST, insertURL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("=======", "DEVICE ID SENT");
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("=======", "DEVICE ID ERROR");
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("id",userID);
            parameters.put("deviceId", token);
            return parameters;
        }
    };
Jozef Dochan
  • 926
  • 10
  • 27