Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
7.7k views
in Technique[技术] by (71.8m points)

php - org.json.JSONException: End of input at character 0 of when i want to use json for get information from database

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
by (100 points)
i am facing the same problem please let me know if found solution thanks
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...