Android Volley BasicNetwork.performRequest() unexpected response code 404
Asked Answered
T

8

7

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.

Tsarevitch answered 15/9, 2016 at 7:39 Comment(4)
Code 404 means the resource does not exist on the server. Is the URL correct?Jolly
en.wikipedia.org/wiki/HTTP_404 This means that the page not found. Check your urlRamberg
Yes Sir henry My URL is correct ,I have checked it on Postman and it is working there.Tsarevitch
have you initialized volley singleton?Felicitasfelicitate
R
12

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.

Rope answered 15/9, 2016 at 7:59 Comment(2)
mam ,I have checked both the parameters and url , they are correct , If I don't override getParams method and use my url like this .api.netdesk.in/api/… , then I get response, I want to know why parameters are not passed using getParams method.Tsarevitch
tnx. it's for Content-type in back-endBaldpate
D
4

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);
Dissect answered 28/3, 2018 at 11:36 Comment(1)
Thanks! I was hitting a get request as post.Biologist
K
1

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

Kathaleenkatharevusa answered 14/2, 2017 at 6:49 Comment(0)
O
1

Removing getHeaders() solved the problem for me.

Outspoken answered 29/9, 2017 at 10:19 Comment(2)
That rather sounds like a comment. You should add more content in order to provide a really helpful answer. You see - that methods needs to implemented somehow. You can't just "remove" it.Privation
Please elaborate.Midpoint
H
1

Probably the local IP in the stringRequest might be wrong please check.I solved my issue by changing my IP.

My IP was:

private static final String ROOT_URL="http://192.168.0.101/Android/functions";
public static final String URL_REGISTER = ROOT_URL+"/registerUser.php";

Where, later i found out that by pasting this IP in my emulator and my browser it wasnt working. I was stuck in this problem for a day. Then i removed the root folder "Android" from the IP and its worked.

So my IP is now: (removed Android folder in the string)

private static final String ROOT_URL="http://192.168.0.101/functions";
public static final String URL_REGISTER = ROOT_URL+"/registerUser.php";

Just keep in mind the IP must be working in both browser and emulator. If it is then use that IP in your project.

Heavily answered 13/9, 2018 at 7:31 Comment(0)
R
0

Try your api simply with this code and check response.

 StringRequest stringRequest = new StringRequest(Request.Method.POST, ApiURL.LOGIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("Response", response); 
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Response", error.toString());
                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("email", email.getText().toString());
            params.put("password", password.getText().toString());
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/x-www-form-urlencoded");
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}
Rope answered 16/9, 2016 at 7:32 Comment(1)
it would be great if you have edit your first answer instead of post a new second answer. !!Uppsala
A
0

Change the POST to GET method, This worked for me:

StringRequest stringRequest = new StringRequest(Request.Method.GET,AppConstants.LOG_IN_API , new Response.Listener<String>() {
Auroreaurous answered 31/8, 2020 at 10:59 Comment(0)
B
0

In my case, everything ok on the android-side but the API returns a 404 response. Plz also check on the postman.

PostMan

Boney answered 14/9, 2020 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.