gpt4 book ai didi

java - 输出html而不是json

转载 作者:行者123 更新时间:2023-11-30 00:38:34 24 4
gpt4 key购买 nike

我一直在做一个关于使用 php 将 android 连接到 mysql 的教程,但我不断收到此错误消息

error parsing data org.json.JOSNException:VALUE<HTML><TITLE>404
type java.lang.String cannot be converted to JSON object

据我所知,php 文件中有问题,有人可以告诉我如何修复它

<?php

/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";

// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";

// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

最佳答案

错误 404 在 HTTP 中表示未找到页面,很可能是 URL 不正确。

将以下内容添加到 PHP 顶部:

header('Content-type: application/json');

关于java - 输出html而不是json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21973062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com