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 !
我想使用Volley库对请求体中的XML格式数据向我的服务器发出一个简单的POST请求。是否可以使用StringRequest实现这一点?提前谢谢!
1 个解决方案
#1
4
It is impossible to use StringRequest
for custom body. But you can either extend StringRequest
or Request
to override getBody()
method.
对自定义主体使用StringRequest是不可能的。但是您可以扩展StringRequest或Request来覆盖getBody()方法。
Here is easiest way to do this:
下面是最简单的方法:
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;
}
}
You might also want to override getBodyContentType()
with something like application/xml
您可能还想用application/xml之类的东西覆盖getBodyContentType()
#1
4
It is impossible to use StringRequest
for custom body. But you can either extend StringRequest
or Request
to override getBody()
method.
对自定义主体使用StringRequest是不可能的。但是您可以扩展StringRequest或Request来覆盖getBody()方法。
Here is easiest way to do this:
下面是最简单的方法:
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;
}
}
You might also want to override getBodyContentType()
with something like application/xml
您可能还想用application/xml之类的东西覆盖getBodyContentType()