1

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?

Jans Rautenbach
  • 394
  • 4
  • 13
  • is `10_000` interpreted as 10s or 10ms? Could it be timing out in 10ms? What happens if you pass in `10000` instead? – nasch Sep 11 '16 at 14:40
  • 1
    This post might help you: http://stackoverflow.com/questions/18866571/receive-json-post-with-php – whitebrow Sep 11 '16 at 14:41
  • Some of the devices/networks block 80(http) port on the network.Chek whether are you able to receive the request in server and make sure for every response add new header such as random number so that network shouldn't cache your request. – Madhukar Hebbar Sep 11 '16 at 15:15

0 Answers0