我目前正在使用 GsonRequest 发出休息 GET 请求。不清楚在我需要发送整个 JSon 对象以进行更新的地方使用什么 PUT 请求。Request 对象将接受 PUT 但我不确定如何放置预期的 JSon 对象。
这是我要放置的json:
{
prop1 : true,
prop2 : false,
prop4 : true
}
以下是它在 apiary.io 中的提交方式,例如:
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'http://my.apiary.io/v1/records/{myid}.json');
xhr.send("{\n \"isEditable\": false,\n \"isClosed\": true,\n \"isAvail\": true\n}");
我不知道把 JSON 放在哪里。
谢谢
public class GsonRequest<T> extends Request<T> {
private final Gson gson ;
private final Class<T> clazz;
private final Map<String, String> headers;
private final Listener<T> listener;
public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers,
Listener<T> listener, ErrorListener errorListener) {
super(method, url, errorListener);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Timestamp.class, new TimestampDeserializer());
this.gson = gsonBuilder.create();
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(
gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
这是请求中的基本 getBody 方法。它似乎处理 Method.PUT 上的参数,但如果它是一个 JSON 字符串需要在正文中发送呢?
/**
* Returns the raw POST or PUT body to be sent.
*
* @throws AuthFailureError in the event of auth failure
*/
public byte[] getBody() throws AuthFailureError {
Map<String, String> params = getParams();
if (params != null && params.size() > 0) {
return encodeParameters(params, getParamsEncoding());
}
return null;
}
/**
* Converts <code>params</code> into an application/x-www-form-urlencoded encoded string.
*/
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
建议的解决方案:
// add a Json body.
public String jsonBody;
/**
* Returns the raw POST or PUT body to be sent.
*
* @throws AuthFailureError in the event of auth failure
*/
public byte[] getBody() throws AuthFailureError {
if ((getMethod() == Method.PUT) && (jsonBody != null))
{
return jsonBody.getBytes(); // Encoding required?????
}
else
{
return super.getBody();
}
}