我正在使用 Volley 从 REST 服务中获取数据。所以有两种方法,一种是 POST,它在服务器上创建一个对象,另一种是 PUT,它基本上更新同一个对象。我使用相同的代码来发出这些请求。我只是改变了在 Volley 中传递的方法。应用程序工作通过在正文中发送正确数据的 POST 请求很好,但是它遇到了 PUT 请求的问题,它随机地在正文中不发送任何数据。
我已按照 Google 的建议改用自定义 Request 对象,但它仍然无法正常工作。
int method = Request.Method.POST;
String objID = sharedPreferences.getString("objID", null);
String Url = "/api_url";
if (objID != null)
{
Url += objID + "/";
method = Request.Method.PUT;
}
try
{
JSONObject objJSON = new JSONObject();
objJSON.put("name", "new_name");
}
catch (JSONException e)
{
// Handle Execption
}
GsonRequest request = new GsonRequest<CustomObj>(method, Url, CustomObj.class,null, null, objJSON.toString(),
new Response.Listener<CustomObj>()
{
@Override
public void onResponse(CartObj cartObj)
{
// Handling Logic
}
}
,
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError volleyError)
{
// Handle Error
}
});
VolleySingleton.getInstance(this).addToRequestQueue(request);
GsonRequest 是我的自定义类扩展请求对象
public class GsonRequest<T> extends Request<T>
{
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Listener<T> listener;
private final Map<String, String> params;
private final String body;
private ErrorListener mErrorListener;
/**
* Make a GET request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson's reflection
* @param headers Map of request headers
*/
public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers, Map<String, String> params, String body, Listener<T> listener, ErrorListener errorListener)
{
super(method, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
this.params = params;
this.body = body;
this.mErrorListener = errorListener;
}
public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers, Map<String, String> params, Listener<T> listener, ErrorListener errorListener)
{
this(method, url, clazz, headers, params, null, listener, errorListener);
}
public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers, Listener<T> listener, ErrorListener errorListener)
{
this(method, url, clazz, headers, null, null, listener, errorListener);
}
@Override
public byte[] getBody() throws AuthFailureError
{
return body != null ? body.getBytes() : super.getBody();
}
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
return params != null ? params : super.getParams();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
return headers != null ? headers : super.getHeaders();
}
@Override
protected void deliverResponse(T response)
{
listener.onResponse(response);
}
@Override
public void deliverError(VolleyError error)
{
mErrorListener.onErrorResponse(error);
}
@SuppressWarnings("unchecked")
@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));
}
}
}
我正在使用 GSON 来解析我的数据,而 CustomObj 是一个用于 volley 的映射对象。
最佳答案
首先确保您正在构建的 url(您检查 objID
是否不为 null)是正确的。
其次,您可以尝试使用字符集调用getBytes()
方法。
在请求中覆盖 getBody()
方法:
@Override
public byte[] getBody() throws AuthFailureError {
try {
return body != null ? body.getBytes(getParamsEncoding()) : super.getBody();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return super.getBody();
}
希望这会有所帮助。
编辑:
您也可以尝试覆盖 getBodyContentType()
方法:
@Override
public String getBodyContentType() {
return "application/json; charset=" + getParamsEncoding();
}
关于java - 通过android Volley使用PUT方法发送空体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28303260/