凌空Content-Type头部没有更新 [英] Volley Content-Type header not updating

查看:178
本文介绍了凌空Content-Type头部没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个排球POST调用,到一个XML主体发送到服务器。我无法正确设置内容类型头。

I am trying to write a POST call in Volley, to send an XML body to the server. I cannot set the Content-Type header correctly.

基本 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;
        }
    };

我重写 getHeaders()供应内容类型的头,我想要的 - 应用程序/ XML

I override getHeaders() to supply the Content-Type header that I want - application/xml.

这是根据建议的问题与此类似:

That is based on the suggestions questions similar to this one:

  • <一个href="http://stackoverflow.com/questions/24022554/android-volley-post-request-header-not-changing">Android凌空POST请求头不改变

在请求被发送,排球已经自动添加了第二个内容类型头,所以标题是这样的:

When the request is sent, Volley has added a second Content-Type header automatically, so the headers look like this:

Content-Type: application/xml
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

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

How do I set the correct header? Or remove the incorrect header?

我试图通过基要求 code跟踪,但一直未能寻找到这些额外的标题从何而来。

I have tried tracing through the base Request code, but have been unable to find where this extra header comes from.

推荐答案

内容类型头是不是同样的方式对待其他的头用乱射。特别是覆盖 getHeaders()更改内容类型并不总是工作。

The Content-Type header is not treated the same way as other headers by Volley. In particular, overriding getHeaders() to change the content type does not always work.

要做到这一点,正确的方法是重写 getBodyContentType()

The correct way to do this is to override getBodyContentType():

    public String getBodyContentType()
    {
        return "application/xml";
    }

我发现这个通过查看code为 JsonRequest 类。

Delyan还提到在他的回答此相关的问题:

Delyan also mentions it in his answer to this related question:

这篇关于凌空Content-Type头部没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆