Reputation: 1921
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.
Upvotes: 7
Views: 52802
Reputation: 2261
In my case, everything ok on the android-side but the API returns a 404 response. Plz also check on the postman.
Upvotes: 0
Reputation: 69
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>() {
Upvotes: 0
Reputation: 546
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.
Upvotes: 1
Reputation: 625
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);
Upvotes: 4
Reputation: 575
By mistake I had put GET request instead of POST, I changed it and that solved my issue of 404 response.
Upvotes: 1
Reputation: 136
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.
Upvotes: 12
Reputation: 136
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);
}
Upvotes: 0