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.toolbox.HttpHeaderParser;
import com.gh.common.util.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

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

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

	public JsonObjectExtendedRequest(int method, String url,
			Response.Listener<JSONObject> listener, ErrorListener errorListener) {
		super(method, url, null, listener, errorListener);
		this.url = url;
	}

	public JsonObjectExtendedRequest(int method, String url,
			String requestBody, Response.Listener<JSONObject> listener,
			ErrorListener errorListener) {
		super(method, url, requestBody, listener, errorListener);
		this.url = url;
	}

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