Android Volley: Unexpected response code 405
Asked Answered
P

3

8

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?

Professed answered 11/11, 2015 at 11:44 Comment(0)
P
14

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);
Professed answered 11/11, 2015 at 11:51 Comment(0)
T
4

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

Ticklish answered 14/8, 2018 at 7:9 Comment(0)
B
0

I had the same issue, and I found out that my API URL was wrong.

So, my suggestion is to recheck the API URL.

Bekah answered 30/9, 2021 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.