package com.gh.gamecenter.volley.extended;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.gh.common.util.Utils;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.UnsupportedEncodingException;

/**
 * 
 * @author 温冠超
 * @email 294299195@qq.com
 * @date 2015-7-31
 */
public class JsonArrayExtendedRequest extends JsonExtendedRequest<JSONArray> {

	private String url;

	public JsonArrayExtendedRequest(String url,
			Response.Listener<JSONArray> listener, ErrorListener errorListener) {
		super(Method.GET, url, null, listener, errorListener);
		this.url = url;
	}

	public JsonArrayExtendedRequest(int method, String url,
			JSONArray jsonRequest, Listener<JSONArray> listener,
			ErrorListener errorListener) {
		super(method, url, (jsonRequest == null) ? null : jsonRequest
				.toString(), listener, errorListener);
		this.url = url;
	}

	@Override
	protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
		Utils.log(url + " = " + response.statusCode);
		try {
			String jsonString = getResponseString(response);
			return Response.success(new JSONArray(jsonString),
					HttpHeaderParser.parseCacheHeaders(response));
		} catch (UnsupportedEncodingException e) {
			return Response.error(new ParseError(e));
		} catch (JSONException je) {
			return Response.error(new ParseError(je));
		}
	}

}
