2
votes

I am creating an android project. For my project I created a REST API using the PHP Slim Framework. When I am opening the URL of the API in my browser it is working fine. See the image below.

enter image description here

But when I am trying to send a request to this URL (shown in the snapshot above) using JsonObjectRequest, I am getting this error in my logcat.

E/Volley: [397] BasicNetwork.performRequest: Unexpected response code 400 for http://192.168.94.1/MyApplication/v1/countrydata

I am using the following code.

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Config.URL_COUNTRY_DATA, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<String,String>();
            params.put("Authorization",Config.API_KEY);
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> headers = new HashMap<String,String>();
            headers.put("Charset", "UTF-8");
            headers.put("Content-Type", "application/x-javascript");
            return headers;
        }
    };
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);

I searched a lot on google and found some cconfusing solutions. Some are saying that you are not using header, some said don't user header etc etc.

I tried all the solutions but none of them worked so far.

1
I am trying to send a request to this URL (shown in the snapshot above) from Android Studio.. From Android Studio? Please explain how you do that as i'm not aware that such is possible.greenapps
I edited it.. but you should have got the problem by seeing the code. :)Belal Khan
in my browser. Where? There are so many browsers on so many devices.greenapps
trying to send a request to this URL. From where?greenapps
response code 400. Well what does this http 400 mean?greenapps

1 Answers

0
votes
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", "Bearer " + strToken);
            return headers;
        }

    };
    RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());
    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(20000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    mRequestQueue.add(jsonObjReq);