ShrimpCrackers ShrimpCrackers - 1 year ago 249
JSON Question

Android HttpURLConnection POST No data being sent

I am running an JSON service on localhost. I'm sending a JSON POST request from an Android app to the server. The server is receiving the connection, but no POST data (I confirm this by setting a breakpoint which hits after I POST from the Android app). The HttpURLConnection response code I get back is 200 OK. However, the data is not being received by the server. I am unsure as to whether any data is being sent. My android code is (wrapped in an AsyncTask):

public void makeHttpRequest(String verb, String serverAddr, String postBody)
HttpURLConnection urlConnection = null;
OutputStream out = null;
try {
serverAddr = "";
URL url = new URL(serverAddr);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");


out = new BufferedOutputStream(urlConnection.getOutputStream());

int responseCode = urlConnection.getResponseCode();
System.out.println("HTTP Response Code: " + responseCode + " | " + urlConnection.getResponseMessage());
catch (MalformedURLException mal) {
catch (IOException ioe) {
finally {
if (out != null) {
try {
} catch(IOException e) {


if (urlConnection != null)


The C#/ASP.NET service contract. instance of Parent is null, but should contain the data sent by POST:

public interface IRestServiceParent

[WebInvoke(UriTemplate = "{dummy}", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Parent PostParent(string dummy, Parent instance);

public Parent PostParent(string dummy, Parent instance)
var result = MasterStorageStore.Instance.PostParent(instance);
if (result == null) return emptyParent;

return NewCopy(result);

Answer Source

I found out through a simple PHP script that for some reason despite setting setRequestMode("POST") and setDoOuput(true), HttpURLConnection was being sent as GET instead of POST. I don't know why.