In volley library giving com.android.volley.Server

2020-06-04 03:35发布

问题:

I try to login in my application, when I try to call API using volley library, it's giving com.android.volley.ServerError and response code 400.

final String username = editTextUsername.getText().toString().trim();
        final String password =   editTextPassword.getText().toString().trim();
        final String email = editTextEmail.getText().toString().trim();

  StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_USERNAME,username);
                params.put(KEY_PASSWORD,password);`enter code here`
                params.put(KEY_EMAIL, email);
                return params;
            }

        };

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

I saw the many examples but I did not get any solution.

回答1:

I had the same issue. It drained my entire day. Finally, I Found the tiny error, which was that I was passing a null value in parameters.

Check if you are passing a null value to the server in getParams() or your JSON Object. If yes, that may be the problem.



回答2:

In below method

public void onErrorResponse(VolleyError error)

add this line:-

error.printStackTrace()

You will get all stack trace which gives you the actual error. I have also get this error but in my case it is actually TimeoutException.



回答3:

Response Code 400 means Bad Request.

Some reasons for that.

  1. There might be issue in Request method. Is it GET or POST.
  2. API might be expecting Authorization Header.


回答4:

What causes this error... 1.Wrong Url,check your url for any errors. 2.Missing parameters,any parameters is having null value or you are missing any parameter. 3.Watch for the Keys defined in the params,it should match with those of APIs. Hope it helps!



回答5:

This error can mean BAD REQUEST, check the headers you are sending. Overring the getHeaders() method you can send the right headers.

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
        return (headers != null || headers.isEmpty()) ? headers : super.getHeaders();
    }

Also, usually the WebServices need application/json headers, as follow:

headers.put("Accept","application/json");
headers.put("Content-Type","application/json");


回答6:

You have to have a same Method in your java codes and in the php codes . you defined "POST" method for your request with volley library to server , now you have to have "POST" method in your php files at your server for giving the request .