我已经在 android studio 中成功设置了 Retrofit 2。通过 php 文件向 mysql 数据库发出 GET 请求时,应用程序显示以下错误。
预期是 begin_array,但在第 1 行第 1 列路径 $ 处是字符串
我曾多次尝试重写 php 文件,但没有成功。请参阅下面我当前的 php 文件。
<?php
header('Content-Type: application/json');
include('conn.php');
$response = array();
//if(isset($_GET['1'])){
//$getid = $_GET['1'];
$read = "SELECT * FROM Cbt";
$result = $conn->query($read);
while ($row = $result->fetch_assoc()) {
$cbtLog = [
$userId = $row['userId'];
$moodBefore = $row['moodBefore'];
$automaticThought = $row['automaticThought'];
$distortions = $row['distortions'];
$challengeThought = $row['challengeThought'];
$alternativeThought = $row['alternativeThought'];
$moodAfter = $row['moodAfter'];
];
array_push($response, $cbtlog)
}
echo json_encode($response);
我希望修复 php 文件中的任何错误,以便结果可以显示在 Android 应用程序上。请帮助我诊断并解决问题。
最佳答案
开始工作了
<?php
include('conn.php');
enter code here
$query = "SELECT * FROM feelingFit_Cbt";
$result = mysqli_query($conn, $query);
$json_array = array();
while ($row = mysqli_fetch_assoc($result)){
$json_array[] =$row;
}
echo json_encode($json_array);
关于php 文件返回错误转换为 Android GET 请求的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56265500/