I saw this code in a tutorial video, and in that video the app is working just fine and even showing the "Login successful" toast.
But when I tried to replicate it, it's just not working for me even if I put the correct username and password that is in the database. (I tried on XAMPP with user's LAN IP as well as Wi-Fi IP and also tried to host the PHP script on 000webhost.com but still not working in any of that methods)
My Android code:
package com.example.devarsh.googlemapsbustracker;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int ERROR_DIALOG_REQUEST = 9001;
private EditText User_name, Pass_word;
private Button btnlogin;
private static String URL_LOGIN =
"http://192.168.50.127/Android/login.php";
//vars
EditText enroll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnlogin = findViewById(R.id.button2);
User_name = findViewById(R.id.username);
Pass_word = findViewById(R.id.password);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Login();
}
});
isServicesOK();
}
private void Login(){
String URL =
"imagebustracker.000webhostapp.com/Android/parul/login.php";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.trim().equals("success")){
Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Selection_Activity.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "Login Unsuccessfull", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Login Unsuccessfull", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("UserName", User_name.getText().toString().trim());
params.put("PassWord", Pass_word.getText().toString().trim());
return params;
}
};
requestQueue.add(stringRequest);
}
public void isServicesOK() {
Log.d(TAG, "isServicesOK: checking google services version");
int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if(available == ConnectionResult.SUCCESS){
//everything is fine and user can make map request
Log.d(TAG, "isServicesOK: Google Play Services is working");
}
else if(GoogleApiAvailability.getInstance().isUserResolvableError(available)){
//there is an error but we can fix it
Log.d(TAG, "isServicesOK: An error is there but we can fix it");
Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(this, available, ERROR_DIALOG_REQUEST);
dialog.show();
}else{
Toast.makeText(this, "We cant make maps request", Toast.LENGTH_SHORT).show();
}
}
}
In the Android code I just added that if the response.trim().equals("success")
then it will start a new activity. But it keeps toasting "Login Unsuccessful".
PHP script:
<?php
$conn =
mysqli_connect("localhost","id7263189_root","dev@1234"
,"id7263189_logintb");
mysqli_query($conn, "SET NAMES 'utf8'");
$username = $_POST["UserName"];
$password = $_POST["PassWord"];
$query = 'SELECT id FROM logintb WHERE username = "'.$username.'" and
password = "'.$password.'"';
$result = mysqli_query($conn,$query) or die('error: ' .mysql_error());
if(mysqli_num_rows($result) == 1){
echo "success";
}else{
echo "error";
}
?>
And currently i'm using XAMPP to connect to myphpadmin. But if i needed i will host this to the 000webhost to check using the real device with the internet.
First of all, test your PHP script. Therefore, hardcode the username and password variable like this
$username = "myUsername";
$password = "myPassword";
Make sure that myUsername and myPassword is data that actually exists in your database. From there, call the same URL in your web browser and look at the response. Is it a success or error.
Another thing worth noting is reading the response code in your Android app when you are making the call. Response code will clearly determine if it's even communicating with the service or not. But in your case, I would recommend you to print out the Volleyerror.
Additionally, provide me with the link you used for this tutorial. Sometimes good to have double set of eyes on the code.
As you've mentioned you are encountered with the 'Login Unsuccessful' toast, it means it's hitting the onErrorResponse function. Therefore, change the function to the following: -
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Login Unsuccessfull", Toast.LENGTH_LONG).show();
Log.e(TAG, error); //or
System.println("VolleyError Start: " + error + " : End VolleyError");
}
And please share the error. Let me know if you have any questions.