PHP服务器不接受Android应用程序的Json数据?

时间:2018-01-10 13:27:52

标签: php android json httpurlconnection

我见过像this这样的类似问题,但他们没有回答我的问题。我试图使用以下代码将android的json数据发送到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();

    }


?>

1 个答案:

答案 0 :(得分:-1)

您可以尝试在Android应用中添加以下内容吗?

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