Not sure if this will help you, but I have a jackson request that takes a type and returns a string, I use it because most of my requests just return a header
Edit
It has different types of requests and responses, it is not super elegant, but it works
public class JacksonJsonRequest<K, T> extends Request<T> { private ObjectMapper mMapper = new ObjectMapper(); private static final String PROTOCOL_CHARSET = "utf-8"; private static final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET); private final Response.Listener<T> mListener; private final K mRequestBody; private final Class<T> mClass; private final Map<String, String> mHeaders; public JacksonJsonRequest(int method, String url, K requestBody, Response.ErrorListener errorListener, Response.Listener<T> listener, Map<String, String> headers, Class<T> claz) { super(method, url, errorListener); mListener = listener; mRequestBody = requestBody; mHeaders = headers; mClass = claz; } @Override protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) { String jsonString = new String(networkResponse.data); try { T result = mMapper.readValue(jsonString, mClass); return Response.success(result, HttpHeaderParser.parseCacheHeaders(networkResponse)); } catch (IOException e) { Log.e("jacksontest", "error parsing", e); } return null; } @Override protected void deliverResponse(T t) { mListener.onResponse(t); } @Override public String getBodyContentType() { return PROTOCOL_CONTENT_TYPE; } @Override public byte[] getBody() { try { return mRequestBody == null ? null : mMapper.writeValueAsBytes(mRequestBody); } catch (JsonProcessingException e) { Log.e("Jacksontest", "error parsing", e); } return null; } @Override public Map<String, String> getHeaders() throws AuthFailureError { return (mHeaders == null) ? super.getHeaders() : mHeaders; }
}
source share