1
votes

I am running an ASP.net 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 = "http://10.0.2.2:4617/parent/dummy";
            URL url = new URL(serverAddr);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setChunkedStreamingMode(0);
            urlConnection.setRequestProperty("Transfer-Encoding","chunked");
            urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

            urlConnection.connect();

            out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(postBody.getBytes("UTF-8"));

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

                }
            }

            if (urlConnection != null)
                urlConnection.disconnect();
        }

    }

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

[ServiceContract]
    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);
        }
2

2 Answers

1
votes

Maybe you should try adding a out.flush(); after out.write(postBody.getBytes("UTF-8"));.

0
votes

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.