-1

I've created a login module on Android Studio with the following code

loginActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
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.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class loginActivity extends AppCompatActivity {

    private EditText username,password;
    private Button btn_login, btn_debug;
    private TextView register;
    private ProgressBar loading;
    private static String URL_LOGIN = "http://192.168.137.1/userv1/login.php";

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

        loading = findViewById(R.id.loading);
        username= findViewById(R.id.usernameBox);
        password= findViewById(R.id.passwordBox);
        btn_login= findViewById(R.id.btn_login);
        btn_debug= findViewById(R.id.debugLogin);
        register = findViewById(R.id.tvRegister);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mUsername = username.getText().toString();
                String mPassword = password.getText().toString();

                if (!mUsername.isEmpty() || !mPassword.isEmpty()) {
                    Login(mUsername, mPassword);
                } else {
                    username.setError("Please insert email");
                    password.setError("Please insert password");
                }
            }
        });

        register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick (View v){
                Intent i = new Intent(loginActivity.this,MainActivity.class);
                startActivity(i);
            }
        });

        btn_debug.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick (View v){
                Intent i = new Intent(loginActivity.this,ApplicantHome.class);
                startActivity(i);
            }
        });
    }

    private void Login(final String username, final String password) {

        loading.setVisibility(View.VISIBLE);
        btn_login.setVisibility(View.GONE);

            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                String success = jsonObject.getString("success");
                                JSONArray jsonArray = jsonObject.getJSONArray("login");

                                if (success.equals("1")) {

                                    for (int i = 0; i < jsonArray.length(); i++) {

                                        JSONObject object = jsonArray.getJSONObject(i);
                                        String username = object.getString("username");
                                         Toast.makeText(loginActivity.this, "Success login \nUsername : " + username, Toast.LENGTH_SHORT).show();
                                        loading.setVisibility(View.GONE);
                                    }
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                                loading.setVisibility(View.GONE);
                                btn_login.setVisibility(View.VISIBLE);
                                Toast.makeText(loginActivity.this, "Error " +e.toString(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            loading.setVisibility(View.GONE);
                            btn_login.setVisibility(View.VISIBLE);
                            Toast.makeText(loginActivity.this, "Error " +error.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
            )
            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("username", username);
                    params.put("password", password);
                    return params;
                }
            };
        RequestQueue requestQueue =  Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        }
    }


And this is my SQL code

<?php

if ($_SERVER['REQUEST_METHOD']=='POST') {



    $username = $_POST['username'];
    $password = $_POST['password'];


   $sql = "SELECT * FROM applicant WHERE username='$username' ";
     require_once 'connect.php';
    $response = mysqli_query($conn, $sql);

    $result = array();
    $result['login'] = array();

  if ( mysqli_num_rows($response) === 1 ) {

        $row = mysqli_fetch_assoc($response);

        if ( password_verify($password, $row['password']) ) {

            $index['username'] = $row['username'];
            $index['id'] = $row['id'];

            array_push($result['login'], $index);

            $result['success'] = "1";
            $result['message'] = "success";
            echo json_encode($result);

            mysqli_close($conn);

        } else {

            $result['success'] = "0";
            $result['message'] = "error";
            echo json_encode($result);

            mysqli_close($conn);

        }

    }

}

?>

Right now, after running the app, it never allows me to go past the login, even with the proper username and password. Its supposed to show a toast text with "success login" with the username. But nothing happens when use the login button. Its weird because the registration activity that I have works fine with similiar coding. It allows me to enter data into the database. user table with some usernames

D. Anders
  • 11
  • 3
  • You should look up what the php script actually outputs. This will tell you in which part (frontend/backend) the error is. Also don't use `?>`. This leads to unwanted output if you accidentally have whitespace characters after it. – colburton Dec 02 '19 at 18:28
  • 1
    Learn about prepared statements to prevent sql injection – Jens Dec 02 '19 at 18:29
  • Where do you sen username and password to the backend? – Jens Dec 02 '19 at 18:33
  • Call the **Login** class using asyncTask link: https://stackoverflow.com/questions/15719942/get-json-in-asynctask-android Then get the **server response** from **onPostExecute()** . Then you can fix the error. If you find any diffiuculties, let me know. – user7418129 Dec 12 '19 at 07:55

1 Answers1

0
  1. Before checking everything you must check your ip address.
  2. If the IP address is correct then you have to check whether the The system in which your are using as Server and the device that you are using to test the app is connected to the same network (I mean,the IP address should be same for both device).
  3. If all the above steps are okay and still not working, then follow this =>
  4. Call the Login class using asyncTask link: Get JSON in AsyncTask Android Then get the server response from onPostExecute() . Then you can fix the error. If you find any diffiuculties, let me know.
user7418129
  • 1,074
  • 14
  • 18