How to fix the error org.json.JSONException: End of input at character 0 of
max-jacob
Posted messages
30
Status
Membre
-
BunoCS Posted messages 436 Registration date Status Modérateur Last intervention -
BunoCS Posted messages 436 Registration date Status Modérateur Last intervention -
Bonjour les experts,
I am new to Android and I am working on a login project via a PHP/MySQL web server using the Volley library, and here it is, during my login attempt, I get this error message: error org.json.JSONException: End of input at character 0 of
This is simply the following tutorial: https://www.youtube.com/watch?v=yS5n40h4Wlg
Your help would be greatly appreciated, thank you in advance
Here is my LoginActivity.class
And now, here is my PHP login script
I am new to Android and I am working on a login project via a PHP/MySQL web server using the Volley library, and here it is, during my login attempt, I get this error message: error org.json.JSONException: End of input at character 0 of
This is simply the following tutorial: https://www.youtube.com/watch?v=yS5n40h4Wlg
Your help would be greatly appreciated, thank you in advance
Here is my LoginActivity.class
public class LoginActivity extends AppCompatActivity { private EditText email, password; private Button btn_login; private TextView link_regist; private ProgressBar loading; private static String URL_LOGIN = "http://xxx.com/login.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); loading = findViewById(R.id.loading); email = findViewById(R.id.email); password = findViewById(R.id.password); btn_login = findViewById(R.id.btn_login); link_regist = findViewById(R.id.link_regist); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String mEmail = email.getText().toString().trim(); String mPass = password.getText().toString().trim(); if(!mEmail.isEmpty() || !mPass.isEmpty()){ Login(mEmail, mPass); }else{ email.setError("Please insert Email"); password.setError("Please insert Password"); } } }); link_regist.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(LoginActivity.this, RegisterActivity.class)); } }); } 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(); Toast.makeText(LoginActivity.this, "Success Login. \nYour Name :" +name+" \nYour Email :" +email, 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("email", email); params.put("password", password); return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); } } And now, here is my PHP login script
<?php if ($_SERVER['REQUEST_METHOD']=='POST') { $email = $_POST['email']; $password = $_POST['password']; require_once 'connect.php'; $sql = "SELECT * FROM users_table 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']; $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); } } } ?> | Message modified by moderation For easier reading of the code, in the future use the tags, SEE THIS PAGE |