-3

I have seen similar question like this but they didn't answer my question. am trying to send json data form android to php server using the following code.

@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;
}

I used the following code to create json data for params[1].

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);

The app is returning HttpURLConnection.HTTP_OK but the receive_json.php is not accepting the json data here is also the php script.

<?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();

    }


?>
Yirga
  • 881
  • 1
  • 12
  • 31
  • 1
    Please clarify what you mean by "not accepting the json data". Are you getting any errors? What debugging have you done? What is the value of `$jsonInput`? What is the value of `$objs`? Do you get inside your `if` check? – Patrick Q Jan 10 '18 at 13:44
  • the app is returning HttpURLConnection.HTTP_OK when sending the json data but the php script above does not accept the json file. – Yirga Jan 10 '18 at 13:59
  • 2
    All you did was repeat exactly what you said before. You didn't clarify anything. I'll ask one more time, what do you mean by "the php script above does not accept the json file"? Scripts do not explicitly "accept" or "reject" input. So please be precise in describing exactly what is happening. And please answer the questions that I asked. – Patrick Q Jan 10 '18 at 14:23
  • this code int httpResponse = httpURLConnection.getResponseCode(); in the app is returning HttpURLConnection.HTTP_OK this means the json data is sent to the php server successfully but am unable to read the received json data using the above php code. – Yirga Jan 10 '18 at 14:33
  • 1
    you repeat it again .... also you PHP code is not returning any JSON – Selvin Jan 10 '18 at 14:43

1 Answers1

-1

Can you please try to add following line in your Android App ?

httpURLConnection.setRequestProperty("accept", "*");
mbakgun
  • 75
  • 5