Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

json - Why json_encode create new line after password_verify php and create error in android studio

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://imgur.com/a/fKi0a72

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...