gson解析复杂对象给出null对象

时间:2015-03-16 12:02:09

标签: gson

我需要解析一个包含另一个自定义对象的自定义对象,我解释一下...... 我有两个自定义对象:Page和Widget,Page对象有一个Widget的HashTable我想解析Json一个用varius Widget创建一个Page数组,我的reqclass是:

public class GsonRequest<T> extends Request<T> {
    private final Gson gson = new Gson();
    private final Class<T> clazz;
    private final Map<String, String> headers;
    private final Response.Listener<T> listener;
    private final Map<String, String> params;


    public GsonRequest(String url, Class<T> classType, HashMap<String, String> params, Response.Listener<T> listener, Response.ErrorListener errorListener)
    {
        super(Method.POST, url, errorListener);

        headers = null;
        this.clazz = classType;
        this.listener = listener;
        this.params = params;
    }

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

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

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return params;
    }

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

        try {

            String json = new String(
                    response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(
                    gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));

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


 GsonRequest<Page[]> req =  new GsonRequest<Page[]>(
                url,
                V10Page[].class,
                params,
                new Response.Listener<Page[]>() {

                    @Override
                    public void onResponse(Page[] response)
                    {


                        List<Page> lista = Arrays.asList(response);


                        //callback.onSuccess(response);

                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                System.out.println("Error:" + error.getLocalizedMessage());

            }
        });

问题是所有的obejcts都是null,哪里是错误?

0 个答案:

没有答案