使用Volley Android库发送XML数据 [英] Send XML data with Volley Android library

查看:105
本文介绍了使用Volley Android库发送XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Volley Library在请求主体中使用XML格式的数据向我的服务器发出一个简单的POST请求。
是否可以使用StringRequest实现?
提前致谢!

I would like to make a simple POST request to my server with XML formatted data in the requests' body using Volley Library. Is it possible to achieve that using StringRequest ? Thanks in advance !

推荐答案

无法使用 StringRequest 用于定制身体。但你可以扩展 StringRequest 请求来覆盖 getBody()方法。

It is impossible to use StringRequest for custom body. But you can either extend StringRequest or Request to override getBody() method.

这是最简单的方法:

public class CustomBodyStringRequest extends StringRequest {
    private final String requestBody;

    public CustomBodyStringRequest(String url, String requestBody, Response.Listener<String> listener,
                                   Response.ErrorListener errorListener) {
        super(Method.POST, url, listener, errorListener);
        this.requestBody = requestBody;
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        byte[] body = null;
        if (!TextUtils.isEmpty(this.requestBody)) {
            try {
                body = requestBody.getBytes(getParamsEncoding());
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Encoding not supported: " + getParamsEncoding(), e);
            }
        }

        return body;
    }
}

您可能还想覆盖 getBodyContentType()类似 application / xml

这篇关于使用Volley Android库发送XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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