Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley: Unexpected response code 405

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?

like image 267
jublikon Avatar asked Nov 11 '15 11:11

jublikon


People also ask

What is http error 405?

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, but the target resource doesn't support this method.

What is error 405 on google?

A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method.

When should I use HTTP 405?

The 405 HTTP Status Code means that the method is not allowed. In the 405 HTTP Status Code, the Request-Line isn't considered the resource distinguished by the Request-URI. The response should incorporate an Allow header containing a rundown of substantial strategies for the mentioned asset.


2 Answers

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);
like image 170
jublikon Avatar answered Sep 28 '22 07:09

jublikon


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

like image 24
saigopi.me Avatar answered Sep 28 '22 06:09

saigopi.me