-1

I'm new in php, now i want to create json_encode and use it as API to my android apps, but after i try it on android, the log say that

Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

and the postman shows one line after the json response. how to eliminate the new line (line two) which is not contain anything.

This is my response in postman:

1 {"kode":1,"pesan":"Login Berhasil"}    
2 

picture spoiler : https://i.sstatic.net/ywlMi.jpg

thank you for all your response afterwards

i have tried to change code in php to fetch the password row several time but always produce second line.

 $konek = new Mysqli($HostName, $HostUser, $HostPass, $DatabaseName) or die(Mysqli_errno());

 if($_SERVER['REQUEST_METHOD'] == 'POST')
 {
    $email = $_POST['email'];
    $password = $_POST['password'];
    $role = $_POST['role'];

    $sql = "SELECT * FROM user WHERE email= '$email'";
    $result = $konek->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
                if(password_verify($password, $row['password'])){
                echo json_encode(array('kode' =>1, 'pesan' => 'Login Berhasil'));
                } else {
                    echo json_encode(array('kode' =>2, 'pesan' => 'Login Gagal. Periksa Email atau password'));
                }
        }
     }
 }
else{
    echo json_encode(array('kode' =>101, 'pesan' => 'Login Error '));
}

This is my response in postman:

1 {"kode":1,"pesan":"Login Berhasil"}    
2 
0

1 Answer 1

2

try to see output in json instead of html because in my Postman it is displayed correctly. you should set Content type of Response of API to json to avoid any issues like this. If doesn't help please comment with output as json in postman.

Sign up to request clarification or add additional context in comments.

7 Comments

i just delete the the space after ?> line
and it is work .. but still produce error in android studio "Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $" what is it ?
in android? add header content type : json ?
While sending the response of Get Request in PHP code ,specify content-type=application/json
Please go through for better understanding stackoverflow.com/questions/28418662/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.