package com.ipz2014.android.net;

import android.util.Log;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class BaseJSONRequest extends JsonRequest<JSONObject> {
    private static final String TAG = BaseJSONRequest.class.getSimpleName();
    private static final String STATUS_OK = "success";

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

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            JSONObject responseObject = new JSONObject(jsonString);
            Log.d(TAG, jsonString);

            boolean hasStatus = responseObject.has(STATUS_OK);

            if (hasStatus) {
                boolean isSuccess = responseObject.getBoolean(STATUS_OK);
                if (!isSuccess) return Response.error(new ServerError());
            }
            return Response.success(responseObject, HttpHeaderParser.parseCacheHeaders(response));

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

@@ -173,47 +173,8 @@
 se;%0A
-import com.android.volley.ServerError;%0A
 impo
@@ -1206,24 +1206,26 @@
 TATUS_OK);%0A%0A
+//
             
@@ -1237,24 +1237,26 @@
 asStatus) %7B%0A
+//
             
@@ -1317,16 +1317,18 @@
 US_OK);%0A
+//
         
@@ -1351,37 +1351,77 @@
 uccess) 
-return Response.error
+ %7B%0A//                    return Response.%0A//                %7D
 (new Ser
@@ -1429,24 +1429,26 @@
 erError());%0A
+//
             
@@ -1742,24 +1742,24 @@
 Error(je));%0A
-
         %7D%0A  
@@ -1761,11 +1761,324 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public String getBodyContentType() %7B%0A        // Read at http://stackoverflow.com/questions/17646036/exectuing-http-post-returns-html-instead-of-json%0A        String type = %22application/x-www-form-urlencoded%22;%0A        Log.d(TAG, %22getBodyContentType() %5B%22 + type + %22%5D%22);%0A        return type;%0A    %7D
 %0A%7D%0A

Fix JsonRequest contentType