Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-Volley : set HTTP Header for JsonArrayRequest

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.

like image 356
b101 Avatar asked Jan 07 '16 07:01

b101


1 Answers

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();
        }
    };
like image 181
Bharatesh Avatar answered Oct 09 '22 02:10

Bharatesh