i see this exception org.json.JSONException: End of input at character 0 of
when i use json for get user information from database...
in android after stringrequest i use try and catch for get the json and in json after get the information i set information in sharedpreference ( my preference have set and get )
below my android codes :
LoginPref loginPref = new LoginPref(getActivity());
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 firstname = object.getString("firstname").trim();
String lastname = object.getString("lastname").trim();
String phone = object.getString("phone").trim();
String email = object.getString("email").trim();
loginPref.setFirstName(firstname);
loginPref.setLastName(lastname);
loginPref.setPhone(phone);
loginPref.setEmail(email);
loginPref.setLoginStatus(true);
startActivity(new Intent(getActivity(),MainActivity.class));
getActivity().finish();
Toast.makeText(getActivity(),
"success login...", Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(), e.toString(), }
and my login script:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$password = $_POST['password'];
require_once 'dbConnect.php';
$sql = "SELECT * FROM users_table WHERE email='$email'";
$response = mysqli_query($connection, $sql);
$result = array();
$result['login'] = array();
if (mysqli_num_rows($response) == 1) {
$row = mysqli_fetch_assoc($response);
if (password_verify($password, $row['password'])) {
$index['firstname'] = $row['firstname'];
$index['lastname'] = $row['lastname'];
$index['phone'] = $row['phone'];
$index['email'] = $row['email'];
array_push($result['login'], $index);
$result['success'] = "1";
$result['message'] = "success";
echo json_encode($result);
mysqli_close($connection);
} else {
$result['success'] = "2";
$result['message'] = "error";
echo json_encode($result);
mysqli_close($connection);
}
}
}
my table in database similar below:
|id|firstname|lastname|phone|email|password|
|1 |john |shelbi |0000 |@ |1111 |
if you can help me at this problem please say me what happened for my code and how can i solve this problem...
question from:
https://stackoverflow.com/questions/65934663/org-json-jsonexception-end-of-input-at-character-0-of-when-i-want-to-use-json-f 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…