1

Can someone please guide me that how to get the header value from the url. I have referred to many tutorials but i could not find any tutorial to get the header value from json. Any help would be highly appreciated.

Here is my code:

JsonArrayRequest obreq = new JsonArrayRequest(Request.Method.GET, JsonURL,




                // The third parameter Listener overrides the method onResponse() and passes
                //JSONObject as a parameter
                new Response.Listener<JSONArray>() {



                    // Takes the response from the JSON request
                    @Override
                    public void onResponse(JSONArray response) {



                        pbHeaderProgress.setVisibility(View.GONE);
                        try {

                            // Retrieves the string labeled "colorName" and "description" from
                            //the response JSON Object
                            //and converts them into javascript objects

                            for (int i = 0; i < response.length(); i++) {


                                JSONObject jresponse = response.getJSONObject(i);
                                String id = jresponse.getString("id");
                                Id.add(id);

                                String auth = jresponse.getString("DJ_author_name");
                                Author.add(auth);


                                String date = jresponse.getString("date_gmt");

                                SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                                java.util.Date date4 = null;
                                try {
                                    date4 = form.parse(date);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }



                                Date.add(newDateStr);


                                JSONObject title = jresponse.getJSONObject("title");
                                String tit = title.getString("rendered");

                                Title.add(tit);


                                JSONObject img = jresponse.getJSONObject("better_featured_image");
                                String pic = img.getString("source_url");
                                Image.add(pic);

                            }


                            // Adds strings from object to the "data" string
                            linear.setVisibility(RelativeLayout.VISIBLE);
                            // Adds the data string to the TextView "results"
                            adapter.notifyDataSetChanged();

                        }

                        // Try and catch are included to handle any errors due to JSON
                        catch (JSONException e) {
                            // If an error occurs, this prints the error to the log
                            e.printStackTrace();
                        }
                    }
                },




                // The final parameter overrides the method onErrorResponse() and passes VolleyError
                //as a parameter
                new Response.ErrorListener() {

                    @Override
                    // Handles errors that occur due to Volley
                    public void onErrorResponse(VolleyError error) {
                        pbHeaderProgress.setVisibility(View.GONE);
                        Toast.makeText(Home.this, "error!!! =)",
                                Toast.LENGTH_SHORT).show();
                        noi.setVisibility(RelativeLayout.VISIBLE);
                        Log.e("Volley", "Error");
                    }
                }


        );



        obreq.setRetryPolicy(new DefaultRetryPolicy(
                30000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        // Adds the JSON object request "obreq" to the request queue
        requestQueue.add(obreq);
Akash
  • 11
  • 1
  • 4

2 Answers2

0

please check djodjos answer here

if you mean you want to get the header values of the json that is returned itself, not the headers of the request itself you can use Gson library it's fast reliable and very easy to use

------- Ok then as djodjos answer

 @Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        JSONObject jsonResponse = new JSONObject(jsonString);
        jsonResponse.put("headers", new JSONObject(response.headers));
        return Response.success(jsonResponse,
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }    }

you need to override parseNetworkResponse when you create the new JsonArrayRequest (obreq ) for example

 StringRequest request=new StringRequest(GET,"url",listener,errorListener){
        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            String totalPages=responseHeaders.get("X-WP-TotalPages");
            return super.parseNetworkResponse(response);
        }
    };
Community
  • 1
  • 1
Bassem Wissa
  • 2,947
  • 1
  • 20
  • 20
0

You can subclass Request (or any of its subclasses) and override the parseNetworkResponse method:

 @Override
    protected Response<Bitmap> parseNetworkResponse(NetworkResponse response)
{ 

     Map<String, String> responseHeaders = response.headers;
}

with reference to this stackoverflow's link

Community
  • 1
  • 1
Abdulqadir_WDDN
  • 658
  • 6
  • 22