Android Volley自定义Request(JSON)

/**
 * 自定义JsonRequest
 * Created by lezg on 2014/8/16.
 */
public class MyJsonRequest extends JsonRequest> {


    public MyJsonRequest(String url,Response.Listener> listener,
                         Response.ErrorListener errorListener) {
        this(Method.GET, url,listener, errorListener);
    }

    public MyJsonRequest(int method, String url,
                         Response.Listener> listener,
                         Response.ErrorListener errorListener) {
        super(method, url, null, listener, errorListener);
    }

    @Override
    protected Response> parseNetworkResponse(NetworkResponse response) {
        try {
            String name = Thread.currentThread().getName();
            System.out.println(name);
            ArrayList result = new ArrayList();
            String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            JSONObject jsonObject = new JSONObject(json).getJSONObject("value");
            JSONArray jsonArray = jsonObject.getJSONArray("items");
            for (int i = 0; i < jsonArray.length(); ++i) {
                JSONObject item = jsonArray.getJSONObject(i);
                News newData = new News();
                newData.setTitle(item.optString("title"));
                newData.setDescription(item.optString("description"));
                newData.setPublishDate(item.optString("pubDate"));
                result.add(newData);
            }
            return Response.success(result, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        }catch (JSONException e){
            return Response.error(new ParseError(e));
        }
    }

}



public class GsonRequest extends Request {

    private final Class mClazz;
    private final Response.Listener mListener;

    public GsonRequest(String url, Class mClazz, Response.Listener listener,
                       Response.ErrorListener errorListener) {
        this(Method.GET, url, mClazz, listener, errorListener);
    }

    public GsonRequest(int method, String url, Class mClazz, Response.Listener listener,
                       Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.mListener = listener;
        this.mClazz = mClazz;
    }

    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            Gson gson = new Gson();
            return Response.success(gson.fromJson(jsonStr, mClazz), HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException e) {
            return Response.error(new ParseError(e));
        }
    }

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


你可能感兴趣的:(Android)