As I have commented, for POJO class, you can create a custom request like the following code because you have to do much work if you use StringRequest.
Here, my POJO class is FileInfo, for example.
public class FileRequest extends Request<FileInfo> {
private final String mRequestBody;
private final Response.Listener<FileInfo> mListener;
private final Response.ErrorListener mErrorListener;
private static final String PROTOCOL_CHARSET = "utf-8";
private static final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET);
public FileRequest(int method, String url, Response.Listener<FileInfo> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.mRequestBody = null;
this.mListener = listener;
this.mErrorListener = errorListener;
}
public FileRequest(String url, String requestBody, Response.Listener<FileInfo> listener, Response.ErrorListener errorListener) {
super(requestBody == null ? Method.GET : Method.POST, url, errorListener);
this.mRequestBody = requestBody;
this.mListener = listener;
this.mErrorListener = errorListener;
}
public FileRequest(int method, String url, String requestBody, Response.Listener<FileInfo> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.mRequestBody = requestBody;
this.mListener = listener;
this.mErrorListener = errorListener;
}
@Override
protected Response<FileInfo> parseNetworkResponse(NetworkResponse response) {
try {
FileInfo fileInfo = new FileInfo();
fileInfo.Size = Long.valueOf(response.headers.get("Content-Length"));
fileInfo.Type = response.headers.get("Content-Type");
fileInfo.Modified = response.headers.get("Last-Modified");
fileInfo.Data = response.data;
return Response.success(fileInfo, HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(FileInfo response) {
mListener.onResponse(response);
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
@Override
public void deliverError(VolleyError error) {
mErrorListener.onErrorResponse(error);
}
@Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
mRequestBody, PROTOCOL_CHARSET);
return null;
}
}
}
Hope this helps!