When I try login using incorrect data in app display this error
org.json.JSONException: End of input at character 0 of
when I use correct data, the progress bar is still loading and doesn't pass to the next activity, in logcat when I use Log.d shown this info
{"login":[],"success":"0","message":"error"}
but Toast didn't display any information or error and the progress bar is still loading. I use MySQL as database and password in database is hashed. I can't figure out this problem I'm still learning.
private void Login(final String email, 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 name = object.getString("name").trim();
String email = object.getString("email").trim();
sessionManager.createSession(name, email);
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
startActivity(intent);
finish();
loading.setVisibility(View.GONE);
}
}
Log.d(TAG, "Info " + response);
} 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();
Log.d(TAG, "Info " + e);
}
Log.d(TAG, "Info " + response);
}
},
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("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
login.php
<?php
if ($_SERVER['REQUEST_METHOD']=='POST'){
$email = $_POST['email'];
$password = $_POST['password'];
require_once 'conn.php';
$sql = "Select name, email, password from firmy where email='$email'";
$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['name'] = $row['name'];
$index['email'] = $row['email'];
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);
}
}
}
?>