dongyushen9063 2014-10-07 07:46
浏览 93
已采纳

JSON响应在PHP版本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.

This is my Java code.

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
$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条回答 默认 最新

  • dongwupei7803 2014-10-07 09:20
    关注

    Change your php script a bit:

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输