Android-Volley : set HTTP Header for JsonArrayRequest
Asked Answered
P

1

7

so I've seen a couple of examples for JsonObjectRequests where either this code has been added

        @Override
        public String getBodyContentType() {
            return "application/json";
        }

and sometimes this Code has been used

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            return headers;
        }

either way, it has always been used in combination with a JsonObjectRequest. I don't really know where to add the @Override of the methods inside my JsonArrayRequest. This is how my JsonArrayRequest looks like

JsonArrayRequest localJReq = new JsonArrayRequest(localURL,
      new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response){
            try{
                for (int i=0; i < response.length(); i++)
                {
                    JSONObject jsonObject = response.getJSONObject(i);
                    String title = jsonObject.getString("Title");
                    data += "Beschreibung: "+title+"\n\n\n";
                }
                output.setText(data);
            }catch (JSONException e){
                e.printStackTrace();
            }

        }
    },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    error.printStackTrace();
                    Toast.makeText(MainActivity.this, "No more Items Available",
                          Toast.LENGTH_LONG).show();
                }
    });

I've tried to add it in {} just behind the last closing ) but that didn't work. I tried putting it inside the Response.Listener which also didn't work. So I'm a little bit confused.

Anyways I think it is really strange that I am making a JsonArrayRequest but since my REST API is accepting HTML & JSON as a response, Volley seems to get the HTML response instead of the JSON. Which is leaving me with no items inside my App. Isn't that kind of weird ? Since it is a JsonRequest shouldn't the Request set the content-type to application/json by its self? I'm hoping that by setting the content-type to application/json that I will get the correct response type.

Pursuant answered 7/1, 2016 at 7:48 Comment(3)
Try adding "Accept" header with "application/json" value inside getHeaders()Catamnesis
@Catamnesis it WORKED!!! You are a lifesaver!!! I'm such an idiot, I put Content-Type and application/json inside the getHeaders() - when all I had to do was change it to Accept and application/json The @Override has to follow the closing parenthesis and be inside curly braces, just like @bharat said in his Answer. Funny thing tho, the getHeaders() Override is enough, I deleted the getBodyContentType() Override. Thank You!!!Pursuant
Content-Type for sending, Accept for receiving :)Catamnesis
E
6

You can call those method at the end of the request and brefore;

    JsonArrayRequest localJReq = new JsonArrayRequest(localURL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
           }) {//here before semicolon ; and use { }.
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return super.getHeaders();
        }

        @Override
        public String getBodyContentType() {
            return super.getBodyContentType();
        }
    };
Eloyelreath answered 7/1, 2016 at 8:28 Comment(2)
that's odd. I actually have tried this but it didn't work.. My App still received the HTML response instead of the JSON response. That is why I'm trying in the first place...Pursuant
it is important to point out that the getHeaders() must be changed to headers.put("Accept, "application/json"); The getBodyContentType() can be removed I guess.. Maybe you could Edit your Answer? and Add those lines for future references..Pursuant

© 2022 - 2024 — McMap. All rights reserved.