Hi. I'm trying to send json data via HttpURLConnection by POST using:
URL url = new URL(serverAddr);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Content-Type",
"application/json;charset=utf-8");
out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(postBody.getBytes("utf-8"));
int responseCode = urlConnection.getResponseCode();
System.out.println("HTTP Response Code: " + responseCode + " | " +
urlConnection.getResponseMessage());
I use the following php script to detect whether it is POST or GET. Even
though I have setDoOutput to true and setRequestMethod to "POST" it always
sends as a GET request. Why? I've tried so many different solutons and it
always sends GET. I know because the following php code will always print
"inside request method GET.
Enter code here...<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
error_log("inside request_method POST\n", 3, "./log.log");
$data = json_decode(file_get_contents("php://input"));
print_r($data);
}
else if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
error_log("inside request_method GET\n", 3, "./log.log");
$data = json_decode(file_get_contents("php://input"));
print_r($data);
}
else {
error_log("no request received\n", 3, "./log.log");
}
?>
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/6d1be495-7772-466f-adce-480476b2a3c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.