php 文件返回错误转换为 Android GET 请求的 JSON

标签 php android mysql arrays json

我已经在 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/

相关文章:

java - 如何用java jar文件复制MySQL数据库

php - 在 Linux 上备份上传的文档

php - 我无法在 php 代码中捕获异常

php - 如何更改 mongodb 中的集合结构?

php - 如何在Yii框架的CDbCriteria()中使用case语句?

android - 如何使用 FLAG_KEEP_SCREEN_ON 在 Flutter 中保持屏幕打开?

android - 录制 fragment 或 View ,输出视频

android - 无法为 Xamarin.Forms 项目安装 GooglePlayServices.Base 包

javascript - 如何使用 mysql 提供的日期获取 "time ago"?

php - 如何使用php获取数组中的json记录?