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.
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.
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. – greenappsin my browser
. Where? There are so many browsers on so many devices. – greenappstrying to send a request to this URL
. From where? – greenappsresponse code 400
. Well what does this http 400 mean? – greenapps