I am writing an app that has to be able to send data via a JSON object and POST to a server. I have tried so many methods, but for some reason the app doesn't send any data at all. The request happens fine otherwise. I even get data back.
Here is part of the code:
JSONObject json = new JSONObject();
try {
json.put("name", "dude");
HttpURLConnection httpCon = (HttpURLConnection) new URL("http://example.com/run.php").openConnection();
httpCon.setRequestMethod("POST");
httpCon.setConnectTimeout(10_000);
httpCon.setReadTimeout(10_000);
httpCon.setRequestProperty("Content-Type","application/json");
httpCon.setRequestProperty("Accept", "application/json");
httpCon.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(httpCon.getOutputStream());
writer.write(json.toString());
writer.flush();
writer.close();
....
and on the server: (php)
<?php
echo "result: ". $_POST["name"];
?>
But all I get back is result:
.
I tried curl to see whether it was a server-side issue, but it gave no problems.
So I guess my question is: WTF?