使用Volley和Gson:解析项目和项目列表

时间:2013-10-06 20:54:46

标签: java android gson android-volley

我从未喜欢Gson的一件事是你必须传递一个Class对象或一个TypeToken,如果你得到一个项目或一个项目列表。现在,当试图与Gson一起使用Volley时,这个问题仍然存在,我正在尝试制作一个可用于这两个方面的GsonRequest类。

我的解决方案非常难看,有两个不同的构造函数:一个获取Class<T>参数,另一个获取Type参数。然后,在parseNetworkResponse中,使用其中一个字段调用gson.fromJson,请记住,必须是null

如何以更好的方式实现这一点? (我不喜欢GsonRequestGsonCollectionRequest几乎相等的类)

我的代码,这里:

public class GsonRequest<T> extends Request<T> {

    private final Gson gson;
    private final Class<T> clazz;
    private final Type type;
    private final Listener<T> listener;
    private final Map<String, String> headers;
    private final Map<String, String> params;

    public GsonRequest(int method, String url, Gson gson, Class<T> clazz, Map<String, String> headers, Map<String, String> params, Listener<T> listener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.gson = gson;
        this.clazz = clazz;
        this.type = null;
        this.listener = listener;
        this.headers = headers;
        this.params = params;
    }

    public GsonRequest(int method, String url, Gson gson, Type type, Map<String, String> headers, Map<String, String> params, Listener<T> listener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.gson = gson;
        this.clazz = null;
        this.type = type;
        this.listener = listener;
        this.headers = headers;
        this.params = params;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return this.headers != null ? this.headers : super.getHeaders();
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return this.params != null ? this.params : super.getParams();
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {

            if (this.clazz != null) {
                return Response.success(
                        this.gson.fromJson(new String(response.data, HttpHeaderParser.parseCharset(response.headers)), this.clazz),
                        HttpHeaderParser.parseCacheHeaders(response));
            } else {
                return (Response<T>) Response.success(
                        this.gson.fromJson(new String(response.data, HttpHeaderParser.parseCharset(response.headers)), this.type),
                        HttpHeaderParser.parseCacheHeaders(response));
            }

        } catch (JsonSyntaxException e) {
            e.printStackTrace();
            return Response.error(new ParseError(e));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(T response) {
        this.listener.onResponse(response);
    }

}

3 个答案:

答案 0 :(得分:4)

我使用以下方法来解析JSON列表。 首先不要在构造函数中发送一个Class,而是从reflect包中传递Type类。

我的班级看起来像这样:

public class DownloadRequest<T> extends Request<T> {

private final Gson gson = new Gson();
private final Type type;
private final Map<String, String> params;
private final Response.Listener<T> listener;

public DownloadRequest(int method, String url, Map<String, String> params, Type type, Response.Listener<T> listener, Response.ErrorListener errorListener) {
    super(method, url, errorListener);
    this.type = type;
    this.params = params;
    this.listener = listener;
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {

    try {
        String json = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
        T parseObject = gson.fromJson(json, type);
        return Response.success(parseObject,HttpHeaderParser.parseCacheHeaders(networkResponse));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void deliverResponse(T t) {
    listener.onResponse(t);
}

}

在调用Request.success方法之前,要设置行T parseObject = gson.fromJson(json, type);非常重要。

答案 1 :(得分:3)

您可以使用TypeToken作为Type参数创建新的GsonRequest。

使用泛型GsonRequest,如GsonRequest

为Gson类创建一个简单的Request ...

new GsonRequest<MyClass>(Request.Method.GET, uriBuilder.build().toString(),
                    MyClass.class, null, mResponseListener, mReponseErrorListener));

或为ArrayList创建一个类型......

Type type = new TypeToken<ArrayList<MyClass>>() {}.getType();
new GsonRequest<ArrayList<MyClass>>(Request.Method.GET, uriBuilder.build().toString(),
                    type, null, mResponseListListener, mReponseErrorListener));

答案 2 :(得分:0)

我使用了Volley的JsonObject请求,并使用Response.ToString()通过Gson将Json String解析为Class。

Gson gson = new Gson();
ClassName obj = gson.fromJson(response.ToString(),ClassName.class);

现在你掌握了所有数据。

相关问题