输出html而不是json

时间:2014-02-23 19:05:06

标签: java php mysql json

我一直在做一个使用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);
}
?>

1 个答案:

答案 0 :(得分:0)

错误404在HTTP中意味着找不到页面的可能性很可能不正确。

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

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