JSON响应在PHP版本5.3.24中不起作用

时间:2014-10-07 07:46:06

标签: java php json

我正在开发一个Java应用程序,我必须将值传递给我的服务器,并从PHP文件(版本5.3.24)接收响应。代码在localhost和其他实时服务器中正常运行其中PHP版本大于 5.3.24

这是我的Java代码。

public static void send() {

try {
// make json string, try also hamburger
String json = "{\"name\":\"Frank\",\"food\":\"pizza\",\"quantity\":3}";

// send as http get request
URL url = new URL("http://www.matjazcerkvenik.si/php/json/pizzaservice.php?order="+json);
URLConnection conn = url.openConnection();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
send();
}

这是我的PHP代码。

<?php
$order = $_GET["order"];
$obj = json_decode($order);
$name = $obj -> {"name"};
$food = $obj -> {"food"};
$quty = $obj -> {"quantity"};
if ($food == "pizza") {
$price = 4000;
} else if ($food == "hamburger") {
$price = 5000;
} else {
$price = 0;
}
$price = $price * $quty;
if ($price == 0) {
$status = "not-accepted";
} else {
$status = "accepted";
}
$array = array("name" => $name, "food" => $food, "quantity" => $quty, "price" => $price, "status"     
=> $status);
echo json_encode($array);
?>

1 个答案:

答案 0 :(得分:1)

稍微改变你的php脚本:

<?php
$order = get_magic_quotes_gpc() ? stripslashes($_GET["order"]) : $_GET["order"];
$obj = json_decode($order);
$name = $obj -> {"name"};
$food = $obj -> {"food"};
$quty = $obj -> {"quantity"};
if ($food == "pizza") {
$price = 4000;
} else if ($food == "hamburger") {
$price = 5000;
} else {
$price = 0;
}
$price = $price * $quty;
if ($price == 0) {
$status = "not-accepted";
} else {
$status = "accepted";
}
$array = array("name" => $name, "food" => $food, "quantity" => $quty, "price" => $price, "status"     
=> $status);
echo json_encode($array);
?>