0

I wan't send post from JsonArrayRequest to server and I found some answer and I trying it..

but I got error like this like this

this is my code

HashMap<String, String> params = new HashMap<String, String>();

public void JSON_DATA_WEB_CALL() {
        jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL, new JSONObject(params),
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

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

                    }
                }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("CUSTOM_HEADER", "Yahoo");
                headers.put("ANOTHER_CUSTOM_HEADER", "Google");
                return headers;
            }
        };
        requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);
    }

how to fix that?

Eggy
  • 522
  • 5
  • 29

1 Answers1

0

Use this custom class.

public class CustomJsonArrayRequest extends JsonRequest<JSONArray> {
/**
 * Creates a new request.
 * @param method the HTTP method to use
 * @param url URL to fetch the JSON from
 * @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
 *   indicates no parameters will be posted along with request.
 * @param listener Listener to receive the JSON response
 * @param errorListener Error listener, or null to ignore errors.
 */
public CustomJsonArrayRequest(int method, String url, JSONObject jsonRequest,
                              Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
            errorListener);
}

@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        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));
    }
}
}

Implementation in your case should be something like this:

HashMap<String, String> params = new HashMap<String, String>();

public void JSON_DATA_WEB_CALL() {
    CustomJsonArrayRequest request = new CustomJsonArrayRequest (Request.Method.POST, GET_JSON_DATA_HTTP_URL, new JSONObject(params),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

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

                }
            }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("CUSTOM_HEADER", "Yahoo");
            headers.put("ANOTHER_CUSTOM_HEADER", "Google");
            return headers;
        }
    };
    requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(request);
}
jlively
  • 735
  • 2
  • 9
  • 29
  • I have error `PROTOCOL_CHARSET` has private access in 'com.andoir.volley.toolbox.JsonRequest'.. how to fix? – Eggy Dec 22 '16 at 03:06
  • Can you post what you have done so far? Also, the stack trace of the error? – jlively Dec 22 '16 at 06:30
  • this the CustomJsonArrayRequest http://pastebin.com/KSFZZB1e , and this the activity http://pastebin.com/s8rx3YDQ – Eggy Dec 22 '16 at 14:47
  • and I tried remove the `PROTOCOL_CHARSET` but the post still can't get in server php – Eggy Dec 22 '16 at 14:51