【问题标题】:Php server is not accepting Json data from android app?PHP服务器不接受来自Android应用程序的Json数据?
【发布时间】:2018-01-10 13:27:51
【问题描述】:

我见过类似this 的问题,但他们没有回答我的问题。我正在尝试使用以下代码将 json 数据从 android 发送到 php 服务器。

@Override
protected Boolean doInBackground(String... params) {

    HttpURLConnection httpURLConnection = null;
    OutputStream outputStream = null;
    BufferedWriter bufferedWriter = null;
    try {
        //params[0] is "http://192.168.137.171/receive_json.php".
        //params[0] is the json data in string.
        URL url = new URL(params[0]);
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setFixedLengthStreamingMode(params[1].getBytes().length);
        httpURLConnection.setRequestProperty("Content-Type", "application/json");
        httpURLConnection.connect();
        outputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
       bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        outputStream.flush();
        bufferedWriter.write(params[1]);
        bufferedWriter.flush();
        int httpResponse = httpURLConnection.getResponseCode();
        if (httpResponse == HttpURLConnection.HTTP_OK) {
            Log.d("TAG", "The respose is HTTP_OK");
            return true;
        }
        if (httpResponse == HttpURLConnection.HTTP_ACCEPTED) {
            Log.d("TAG", "The respose is HTTP_ACCEPTED");
            return true;
        }
        if (httpResponse == HttpURLConnection.HTTP_BAD_GATEWAY) {
            Log.d("TAG", "The respose is HTTP_BAD_GATEWAY");
            return false;
        }
        if (httpResponse == HttpURLConnection.HTTP_NOT_FOUND) {
            Log.d("Tag", "The resposnse is HTTP_NOT_FOUND");
            return false;
        }

        outputStream.close();

        //bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

我使用以下代码为 params[1] 创建 json 数据。

JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();                    
 for (Record record : list) 
  {
try {                        
 jsonObject.put("firstName", record.getFirstName());
 jsonObject.put("lastName", record.getLastName());
 jsonObject.put("age", String.valueOf(record.getAge()));
 jsonObject.put("sex", record.getSex());
 jsonObject.put("comment", record.getComment());
 jsonArray.put(jsonObject);
    } 
catch (JSONException e) 
    {
          e.printStackTrace();                              
     }    
   }
 String message = jsonArray.toString();
 String sendTo = "http://192.168.137.171/receive_json.php"
SynchronizeData synchronizeData = new SynchronizeData(getActivity());
synchronizeData.execute(sendTo, message);

应用程序正在返回 HttpURLConnection.HTTP_OK 但 receive_json.php 不接受 json 数据这里也是 php 脚本。

<?php
   echo "php started";

    $jsonInput = file_get_contents('php://input');
    $objs = json_decode($jsonInput);
    if (is_array($objs)) {
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "Synch";

            // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            }

    foreach ($objs as  $obj) {
        $firstName = $obj->firstName;
        $lastName = $obj->lastName;
        $age = $obj->age;
        $sex = $obj->sex;
        $comment = $obj->comment;
        echo $firstName;
        echo $lastName;
        echo $age;
        echo $sex;
        echo $comment;


        $sql = "INSERT INTO Synchdata (firstName, lastName, age, sex, comment)
                VALUES ($firstName, $lastName, $age, $sex, $comment)";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
}
        $conn->close();

    }


?>

【问题讨论】:

  • 请澄清您所说的“不接受 json 数据”是什么意思。你有任何错误吗?你做了什么调试? $jsonInput 的值是多少? $objs 的值是多少?你进入你的if支票吗?
  • 应用程序在发送 json 数据时返回 HttpURLConnection.HTTP_OK 但上面的 php 脚本不接受 json 文件。
  • 你所做的只是重复你之前所说的话。你没有澄清任何事情。我再问一次,“上面的php脚本不接受json文件”是什么意思?脚本不会明确地“接受”或“拒绝”输入。因此,请准确描述正在发生的事情。请回答我提出的问题。
  • 这段代码 int httpResponse = httpURLConnection.getResponseCode();在应用程序中返回 HttpURLConnection.HTTP_OK 这意味着 json 数据已成功发送到 php 服务器,但无法使用上述 php 代码读取接收到的 json 数据。
  • 你再重复一遍......你的PHP代码也没有返回任何JSON

标签: php android json httpurlconnection


【解决方案1】:

您能否尝试在您的 Android 应用程序中添加以下行?

httpURLConnection.setRequestProperty("accept", "*");

【讨论】:

  • 这应该是一个问题还是一个答案?
  • 还有这会如何改变什么?
猜你喜欢
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 2013-03-09
  • 2012-12-08
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多