Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley BasicNetwork.performRequest() unexpected response code 404

I am getting this error while making a network request using volley library,I have followed these links Android Volley - BasicNetwork.performRequest: Unexpected response code 400 and Unexpected response code 404 volley but none of them working in my case . Here is my request code

 StringRequest stringRequest = new StringRequest(Request.Method.POST,AppConstants.LOG_IN_API , new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("TAG", "Login Response: " + response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                   Log.v("USerid",""+jsonObject.getInt("userid"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("volley Error ................."+volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
               Log.v("getparams","Is called");
                Map<String, String> params = new HashMap<String, String>();
                params.put(AppConstants.USER_ID, "[email protected]");
                params.put(AppConstants.PASSWORD, "123456");
                return params;
            }


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();

                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }

        };

        Log.v("Request",""+stringRequest);

        AppController.getInstance().addToRequestQueue(stringRequest);

    }

Is there anyone who can tell me where I am doing wrong in my code? Any help is appreciable.

like image 306
Sandeep dhiman Avatar asked Sep 15 '16 07:09

Sandeep dhiman


3 Answers

Generally this response is caused by sending wrong parameters with the URL. Please check every parameter with spelling and make sure if response is obtained in string or in object. Also check Content-type.

Hope this will help you.
Thank you.

like image 99
Priya Darshani Avatar answered Oct 26 '22 12:10

Priya Darshani


If GET is working and POST is not, and you get 404 response it means that your post request can't be routed by the server. I had the same problem and the solution was simple. I've made my server in NodeJS and I didn't handle POST action for my URL. I was testing it with get and everything was fine.

My test method on node server with Express was:

app.get('/data/helloWorld', routeData.helloWorld);

And of course I should add a route for post method.

app.post('/data/helloWorld', routeData.helloWorld);
like image 40
Daniel Więcek Avatar answered Oct 26 '22 11:10

Daniel Więcek


By mistake I had put GET request instead of POST, I changed it and that solved my issue of 404 response.

like image 1
Spiker Avatar answered Oct 26 '22 10:10

Spiker