Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Volley Android库发送XML数据_Android_Xml_Http_Post_Android Volley - Fatal编程技术网

使用Volley Android库发送XML数据

使用Volley Android库发送XML数据,android,xml,http,post,android-volley,Android,Xml,Http,Post,Android Volley,我想使用Volley库在请求主体中使用XML格式的数据向我的服务器发出一个简单的POST请求。 使用StringRequest可以实现这一点吗? 提前谢谢 无法对自定义主体使用StringRequest。但您可以扩展StringRequest或请求重写getBody方法 以下是最简单的方法: public class CustomBodyStringRequest extends StringRequest { private final String requestBody;

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

无法对自定义主体使用StringRequest。但您可以扩展StringRequest或请求重写getBody方法

以下是最简单的方法:

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;
    }
}
您可能还想用application/xml之类的内容覆盖getBodyContentType。不可能对自定义主体使用StringRequest。但您可以扩展StringRequest或请求重写getBody方法

以下是最简单的方法:

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

您可能还想用application/xml之类的内容覆盖getBodyContentType,谢谢您的回答。因此,如果我理解的话,requestBody参数是字符串化xml数据?是的,您可以指定字符串化xml数据。实际上,它将按原样将任何文本数据发送到请求正文中的服务器。我以为您希望以这种方式使用StringRequest。如果您想生成xml或使其更适合xml请求,您可以扩展请求并使用DOM解析器从映射、列表等生成xml。谢谢您的回答。因此,如果我理解的话,requestBody参数是字符串化xml数据?是的,您可以指定字符串化xml数据。实际上,它将按原样将任何文本数据发送到请求正文中的服务器。我以为您希望以这种方式使用StringRequest。若要生成xml或使其更适合xml请求,可以扩展请求并使用DOM解析器从映射、列表等生成xml。