简体   繁体   English

JsonWriter POST在Android to WCF Web服务中不起作用

[英]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? 我想知道任何人都有关于如何使用JsonWriter将JSON数据从Android发布到WCF Web服务的示例代码?

I tested my WCF with Fiddler 4 (Composer with POST json data) and it gave me the correct return. 我用Fiddler 4(带有POST json数据的Composer)测试了WCF,它给了我正确的回报。
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.). 但是,当我对使用JsonWriter的Android应用程序进行测试时,我没有看到对Fiddler的任何操作(顺便说一句,我设置了Fiddler来检查我的Android Emulator网络流量,顺便说一下,我正在Android Emulator上进行测试。)。

With the same Android application, I can call GET with JsonReader to my WCF and get the correct reply. 使用相同的Android应用程序,我可以使用JsonReader调用GET到我的WCF并获得正确的答复。
Its just calling POST with JsonWriter got no response code or no action in Fiddler. 它仅使用JsonWriter调用POST就没有响应代码或Fiddler中没有任何动作。

For JsonWriter (and Reader), I refer to Android developer >> JsonWriter 对于JsonWriter(和阅读器),我指的是Android开发人员>> JsonWriter

Here are my test results (Get and Post) with Emulator GET and POST. 这是我在模拟器GET和POST下的测试结果(获取和发布)。

JSON_GET

JSON_POST

Here are my test results with Fiddler direct POST. 这是我在Fiddler Direct POST中的测试结果。
First it gave me Result 307 then follow by 200. 首先它给了我结果307,然后是200。

Fiddler_307

Fiddler_200

And here is how I use JsonWriter to post (this block was from AsyncTask). 这就是我使用JsonWriter进行发布的方式(此块来自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). 我尝试了但仍然无法弄清楚为什么JsonWriter没有触发我的WCF(我将WCF附加到了我的本地主机服务,只有Fiddler直接POST会在我的WCF项目中达到断点,而Android App没有达到它)。 I follow the exact example from Android Developer site though. 我从Android Developer网站遵循了确切的示例。 I google and didn't find any site on using JsonWriter with OutputStreamWriter (I saw some post using StringWriter). 我在Google上搜索并没有发现将JsonWriter与OutputStreamWriter一起使用的任何站点(我看到了一些使用StringWriter的帖子)。
May I know where did my code wrong ? 我可以知道我的代码在哪里出错吗?

Based on this StackOverFlow post WCF has a 'Thing' about URI , I managed to solve this issue. 基于这个StackOverFlow帖子, WCF有了关于URI的“东西” ,我设法解决了这个问题。

All I need is to make sure my POST web service has URI Template ends with "Slash". 我需要做的就是确保POST Web服务的URI模板以“斜杠”结尾。

Example: http://10.72.137.98/myWebSvc/posvctFun / 示例: http//10.72.137.98/myWebSvc/posvctFun /
Instead of http://10.72.137.98/myWebSvc/postFun 代替http://10.72.137.98/myWebSvc/postFun

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM