JsonRequest VS StringRequest in Android Volley

你。 提交于 2019-12-12 19:13:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!