Gsingh Gsingh - 20 days ago 10
JSON Question

Can not Make Android Login Using PHP, MySQL and Volley

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);
?>

Answer

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;
        }
    };
Comments