0

I am trying to send a POST request from an Android application using HttpUrlConnection, but I am getting either an empty $_POST in my php script or my JSON data in an odd format.

I believe the issue lies with the way I am setting the RequestProperty Content type, because when I set it to "application/json; charset=UTF-8" $_POST is empty, and when I set it to "application/x-www-form-urlencoded; charset=utf-8" $_POST is {"{\"forename\":\"Joe\",\"surname\":\"Smith\"}":""}.

Ideally I'd like to to be in the format {"forename":"Joe","surname":"Smith"}, as my php script processes the data as $_POST["forename"}, $_POST["surname"}, etc..

EDIT: Played around and found that if I do

$raw_post = file_get_contents('php://input');

in my PHP code, then $raw_post will be {"forename":"Joe","surname":"Smith"}, which is the desired effect. However, $_POST is still empty. Although $raw_post works, it feels like a hacky workaround. Wouldn't using $_POST be the proper way to do this?

EDIT #2: I ended up implementing a final solution/workaround which is:

$raw_post = json_decode(file_get_contents('php://input'), true);

The "true" argument decodes the input into a PHP array, so then I am able to access key-values as $raw_post["forename"]. This still does not solve accessing via $_POST so I will leave this question open, but it is a sufficient workaround.

My Java code is as follows...

class postToServer extends AsyncTask<String, String, String>
{
    @Override
    protected String doInBackground(String... args) {
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL(url_create_customer);
            con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            //con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestMethod("POST");

            JSONObject custy = new JSONObject();
            custy.put("forename", args[0]);
            custy.put("surname", args[1]);

            OutputStream os = con.getOutputStream();
            os.write(custy.toString().getBytes("UTF-8"));  
            os.close();

            //display what returns the POST request
            int HttpResult = con.getResponseCode();
            System.out.println(HttpResult);
            if (HttpResult == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(con.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                br.close();
                System.out.println("" + sb.toString());
            } else {
                System.out.println(con.getResponseMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.disconnect();
        }
        return sb.toString();
    }
}
Tom D
  • 41
  • 2
  • 6
  • Pretty similar, but I'm hesitant to actually mark as a duplicate: https://stackoverflow.com/questions/18866571/receive-json-post-with-php – Patrick Q Oct 25 '17 at 13:01
  • I thought it had more to do with my Java rather than PHP as changing con.setRequestProperty() gets me different results. – Tom D Oct 25 '17 at 14:05

0 Answers0