droirqk4795 2017-10-08 23:15
浏览 38
已采纳

在PHP中读取从android发送的JSON对象?

i'm trying to create an php script which will data sent from the android to an text file but i'm unable to get the output as there is some error in the php here is the php code:-

 <?php
    if($_SERVER["REQUEST_METHOD"]=="POST")
    {

        $data=$_POST["OrderSummary"];
        $json=json_decode($data,true);
        file_put_contents("text.txt",$json);

    }?>

can any tell me where iam going wrong and how can i corect it ?

  • 写回答

1条回答 默认 最新

  • dousu1900 2017-10-09 00:40
    关注

    You can try this code it will give you more information about what is going on in your script.

    Then check the response in you android code.

    <?php
     // array for JSON response
    $response = array();
    
    // Check request method -- for some reason??
    if($_SERVER["REQUEST_METHOD"]=="POST"){
    
        // check for required fields
        if (isset($_POST['OrderSummary'])) {
    
            $data = $_POST['OrderSummary'];
            $json = json_decode($data,true);
    
            $my_file = 'text.txt';
    
            $result = file_put_contents($my_file,$json);   
            $response["success"] = 1;
            $response["message"] = "Number of bytes written = " .$results;
        }
        else{
            $response["success"] = 0;
            $response["message"] = "Parameters not correctly formatted";
        }
    else{
        $response["success"] = 2;
        $response["message"] = "Somebody is not using the POST request method!";
    }
    
    echo json_encode($response);
    ?>
    

    Edit:

    I'm not quite sure why you are checking the request method. But I kept it in the PHP script anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现