使用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