简体   繁体   English

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

[英]JSON response is not working in PHP version 5.3.24

I am developing a Java application where I have to pass values to my server and receive the response from PHP file (version 5.3.24) .The code is running fine in localhost and other live servers where the PHP version is greater than 5.3.24 . 我正在开发一个Java应用程序,必须将值传递到服务器并接收来自PHP文件(版本5.3.24)的响应。代码在localhost和其他PHP版本大于5.3.24的活动服务器中运行良好。

This is my Java code. 这是我的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();
}

This is my PHP code. 这是我的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);
?>

Change your php script a bit: 稍微更改您的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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM