3

我在 POST 请求和在模拟器上运行时遇到问题。

设备问题:发送 POST 请求时,我收到此 411 错误(需要内容长度)。添加该请求属性没有帮助。

模拟器问题:向 Web 客户端键入 REST url 时,它可以工作。但是当使用应用程序客户端运行它时,我得到了 UnkownHostExpection。

到目前为止,我对如何解决我的问题一无所知,所以我希望你能指出一些解决这些问题的技巧。

谢谢。

以下是相关代码:

    URL url = new URL("http://etc..");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.addRequestProperty("Accept", "application/json");
    conn.addRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Accept-Charset", CHARSET_UTF8);

    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setChunkedStreamingMode(0);
    conn.getOutputStream().write(myPostParams.getBytes(CHARSET_UTF8));

    conn.connect();
4

2 回答 2

1

您是否尝试过设置使用 setFixedLengthStreamingMode() 而不是 setChunkedStreamingMode(0)?

于 2013-01-24T08:46:48.690 回答
0

这个“ setFixedLengthStreamingMode()”对我有用

于 2018-06-21T06:16:10.700 回答