Volley Content-Type标头未更新

Ric*_*ier 6 android android-volley

我正在尝试在Volley中编写POST调用,以将XML正文发送到服务器.我无法Content-Type正确设置标题.

基本StringRequest看起来像这样:

StringRequest folderRequest =
        new StringRequest(Method.POST, submitInterviewUrl, myListener, myErrorListener)
    {
        @Override
        public byte[] getBody() throws AuthFailureError
        {
            String body = "some text";
            try
            {
                return body.getBytes(getParamsEncoding());
            }
            catch (UnsupportedEncodingException uee)
            {
                throw new RuntimeException("Encoding not supported: "
                        + getParamsEncoding(), uee);
            }
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError
        {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/xml");
            return headers;
        }
    };
Run Code Online (Sandbox Code Playgroud)

我重写getHeaders()以提供Content-Type我想要的标题 - application/xml.

这是基于与此类似的建议问题:


发送请求后,Volley会Content-Type自动添加第二个标头,因此标题如下所示:

Content-Type: application/xml
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Run Code Online (Sandbox Code Playgroud)

如何设置正确的标题?或删除不正确的标题?

我试过跟踪基本Request代码,但一直无法找到这个额外的标头来自哪里.

Ric*_*ier 27

所述Content-Type标头不处理的相同方式,通过凌空其它标题.特别是,重写getHeaders()以更改内容类型并不总是有效.

执行此操作的正确方法是覆盖getBodyContentType():

    public String getBodyContentType()
    {
        return "application/xml";
    }
Run Code Online (Sandbox Code Playgroud)

我通过查看JsonRequest类的代码找到了这个.

Delyan在回答这个相关问题时也提到了这个问题:

  • 你节省了一天,因为正如你所提到的,getHeaders()并不总是有效 (2认同)