JsonRequest VS StringRequest in Android Volley
Asked Answered
B

2

2

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

Breach answered 6/9, 2015 at 4:51 Comment(0)
B
6

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

Blastomere answered 6/9, 2015 at 5:14 Comment(0)
P
0

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!

Puttergill answered 6/9, 2015 at 7:26 Comment(4)
Thansk. But what benefit does it give?Breach
@AshwaniK: it will return a generic JsonElement, then you can check if it is a JsonArray or JsonObject. Of course, if you don't want so, you can create a class that extends Request<T> :)Puttergill
I think there is some confusion about the question. My question was for json request we can make String request as I use GSON to convert it to POJO.Breach
IMO, if StringRequest, because of onResponse(String response), you must do much work to parse it and convert it to POJO. Why don't you use a custom request that extends Request<Your POJO> ?Puttergill

© 2022 - 2024 — McMap. All rights reserved.