I am a beginner in Android development, My app requires users to enter their email and password in the LoginActivity
in order to login. If the credentials are okay, then the app should start the WorkActivity
.
Here is the PHP script. For testing, I have just set $email
and $password
as static values rather than grabbing them from a database.
<?php
$response = array ("success"=> 0, "responseMessage"=> "Incorrect email/password combination");
$email = "mwangicj";
$password = "asd";
if($_SERVER['REQUEST_METHOD']=='POST'){
$request = json_decode(file_get_contents('php://input'));
if(!strcmp($email,$request->email)&& !strcmp($password,$request->password)){
$response['success'] = 1;
$response['responseMessage'] = "Login successful ".$request->email;
}
}
echo json_encode($response);
?>
I am using Volley library to send and receive data to and from the PHP. Here is the LoginActivity
package com.mwangicj.Lugayocontributor;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class LoginActivity extends AppCompatActivity {
EditText loginEmail, loginPassword;
TextView message;
Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginEmail = (EditText) findViewById(R.id.loginEmail);
loginPassword = (EditText) findViewById(R.id.loginPassword);
loginButton = (Button) findViewById(R.id.loginButton);
message = (TextView) findViewById(R.id.message);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (connected()){
message.setText("");
String email = loginEmail.getText().toString();
String password = loginPassword.getText().toString();
if (!email.isEmpty() && !password.isEmpty()){ //Check if fields are filled in
logInUser();
}else{
Toast.makeText(getApplicationContext(), "Supply both email and password!",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), "No internet", Toast.LENGTH_SHORT).show();
}
}
});
}
//executing user's login request
private void logInUser() {
postJsonRequest("http://10.0.2.2:1234/app/json.php");
}
//Starting work activity
void goToWork(){
Intent i = new Intent(getApplicationContext(), WorkActivity.class);
startActivity(i);
finish();
}
//preparing user's login details
private void postJsonRequest(String url) {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
JSONObject params = new JSONObject();
try {
params.put("email", loginEmail.getText().toString());
params.put("password", loginPassword.getText().toString());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, url, params, new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response) {
try {
String success = response.getString("success");
String responseMessage = response.getString("responseMessage");
if (success == "0"){
message.setText(responseMessage);
}else{
Toast.makeText(getApplicationContext(), responseMessage, Toast.LENGTH_LONG).show();
goToWork();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
message.setText("Error: " + error.toString());
}
});
queue.add(jsonObjectRequest);
}
//Checking internet connection
boolean connected (){
ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cManager.getActiveNetworkInfo();
if(nInfo!=null && nInfo.isConnected()){
return true;
}else{
return false;
}
}
}
The problem is the WorkActivity is starting regardless of correct or incorrect email and password combination.
How do I fix this?