Search code examples
androidjsonpostlocalhostwcf-rest

JsonWriter POST not working in Android to WCF web service


I would like to know anyone has a sample code on how to use JsonWriter to post JSON data to WCF web service from Android?

I tested my WCF with Fiddler 4 (Composer with POST json data) and it gave me the correct return.
However, when I tested with my Android application which use JsonWriter, I didn't see any action on Fiddler (I set up Fiddler to check on my Android Emulator network traffic, by the way, I am testing on Android Emulator.).

With the same Android application, I can call GET with JsonReader to my WCF and get the correct reply.
Its just calling POST with JsonWriter got no response code or no action in Fiddler.

For JsonWriter (and Reader), I refer to Android developer >> JsonWriter

Here are my test results (Get and Post) with Emulator GET and POST.

JSON_GET

JSON_POST

Here are my test results with Fiddler direct POST.
First it gave me Result 307 then follow by 200.

Fiddler_307

Fiddler_200

And here is how I use JsonWriter to post (this block was from AsyncTask).

try
{
    Log.d("TEST_JSON", "URL: " + params[0]);
    URL url = new URL(params[0]);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Accept","application/json");
    conn.setRequestProperty("Content-Type","application/json");

    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);

    // conn.connect();

    OutputStream out = conn.getOutputStream();
    JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));

    try
    {
        writer.setIndent("  ");

        if(params[1].trim() == "ARRAY")
        {
            // Write array to WCF.
        }
        else if(params[1].trim() == "OBJ")
        {
            // Write object to WCF. <<== I am testing with one object.
            writer.beginObject();

            writer.name("ShipNo").value("SI10101");
            writer.name("DoNo").value("DO230401");
            writer.name("PartNo").value("102931-1201");
            writer.name("Qty").value(1);
            writer.name("ShipIn").value(1);

            writer.endObject();
        }
    }
    finally
    {
        writer.close();
        out.close();
    }


    // If I enable below blocks, I will see 307 response code in Fiddler.
    /*
    conn.connect();

    int responseCode = conn.getResponseCode();
    Log.d("TEST_JSON", "Code: " + String.valueOf(responseCode));
    */

    Log.d("TEST_JSON", "Finish sending JSON.");

    conn.disconnect();
}
catch (IOException e)
{
    Log.e("TEST_JSON",e.getMessage());      // <<-- No error from this try catch block.
}

I tried and still cannot figure out why JsonWriter didn't trigger to my WCF (I attached my WCF to my localhost service, only Fiddler direct POST will hit the break point in my WCF project while Android App didn't reach to it). I follow the exact example from Android Developer site though. I google and didn't find any site on using JsonWriter with OutputStreamWriter (I saw some post using StringWriter).
May I know where did my code wrong ?


Solution

  • Based on this StackOverFlow post WCF has a 'Thing' about URI, I managed to solve this issue.

    All I need is to make sure my POST web service has URI Template ends with "Slash".

    Example: http://10.72.137.98/myWebSvc/posvctFun/
    Instead of http://10.72.137.98/myWebSvc/postFun