问题
I am using Android Volley for network calls. Generally I use JSONRequest to receive the json data and then convert them into object using GSON.
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
///Convert response.toString() to POJO using GSON
}
};
If I use plain string request and then convert string to objects using GSON, will that be more faster than JSONRequest?
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
///Convert response to POJO using GSON
}
};
Thanks
回答1:
It would be more efficient to use StringRequest because the raw data returned is in String format, JSONRequest convert the String into JSONObject which is not necessary for your case.
Actually you may implement your own GSONRequest, you can google GSON volley for many references.
Here is one example: making a GSON request using volley
回答2:
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!
来源:https://stackoverflow.com/questions/32420158/jsonrequest-vs-stringrequest-in-android-volley