Android Volley: Unexpected response code 405

17,762

Solution 1

The issue was that the request was set to POST by default. The solution that worked for me:

  JsonObjectRequest jsonReq = new JsonObjectRequest
                            (Request.Method.GET, URL_FEED, null, new Response.Listener<JSONObject>()
                            {
                                @Override
                                public void onResponse(JSONObject response)
                                {
                                    Log.d("Server", "Läuft");
                                }
                            },
                                    new Response.ErrorListener()
                                    {
                                        @Override
                                        public void onErrorResponse(VolleyError error)
                                        {
                                            Log.d("Server","onErrorResponse");
                                        }
                                    });
                    NetworkController.getInstance().addToRequestQueue(jsonReq);

Solution 2

Use GET Method instead of POST it has worked for me.

Share:
17,762
jublikon
Author by

jublikon

Java developer in Düsseldorf, Germany

Updated on June 13, 2022

Comments

  • jublikon
    jublikon almost 2 years

    My Android Volley JsonObjectRequest runs into onErrorResponse with the issue:

    BasicNetwork.performRequest: Unexpected response code 405 for MY_URL
    

    My URL is valid. I have checked that with a browser and I get there the expected JSON Object. So the issue has to be on the client side.

    The code 405 means:

    Method Not Allowed The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

    my code for JsonObjectRequest:

    JsonObjectRequest jsonReq;
                jsonReq = new JsonObjectRequest(URL_FEED, new JSONObject(),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                try {
                                    VolleyLog.v("Response:%n %s", response.toString(4));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.v("ERROR:%n %s", error.getMessage());
                    }
                });
    
                // Adding request to volley request queue
                NetworkController.getInstance().addToRequestQueue(jsonReq);
    

    Do I have to add some information to the header? And if what for information?