Android Volley中的JsonRequest VS StringRequest

时间:2015-09-06 04:51:12

标签: android android-volley

我正在使用Android Volley进行网络通话。通常我使用JSONRequest接收json数据,然后使用GSON将它们转换为对象。

new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                       ///Convert response.toString() to POJO using GSON
                    }
                };

如果我使用普通字符串请求然后使用GSON将字符串转换为对象,那么它会比JSONRequest更快吗?

new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                  ///Convert response to POJO using GSON
                    }
                };

由于

2 个答案:

答案 0 :(得分:6)

使用StringRequest会更有效,因为返回的原始数据是字符串格式,JSONRequest将字符串转换为JSONObject,这对您的情况不是必需的。

实际上,您可以实施自己的GSONRequest,您可以google GSON volley获取许多参考资料。

这是一个例子: making a GSON request using volley

答案 1 :(得分:0)

正如我所评论的,对于POJO类,您可以创建一个自定义请求,如下面的代码,因为如果使用StringRequest,您必须做很多工作。

这里,我的POJO类是FileInfo,例如。

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;
        }
    }
}

希望这有帮助!