0

Volley request showing com.android.volley.ServerError also it is neccessary to implement getHeaders() method? What is exact use of this method?

  send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                    (Request.Method.POST, URL, null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                        }
                    }){
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("username","Admin");
                    params.put("password", "123456789");

                    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(getApplicationContext());
            requestQueue.add(jsonObjectRequest);
        }
    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
suraj shinde
  • 230
  • 1
  • 7
  • 19

1 Answers1

0

Generally this type of error occurs when you are using poor internet connection or else your server goes down. There is no need to implement getHeaders() method. Try to just check your connection or server connection.

Use the android volley library,

 compile 'com.android.volley:volley:1.0.0'


        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "Error: " + error.getMessage());
                }
            }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
             params.put("username","Admin");
             params.put("password", "123456789");
            return params;
        }

    };

     RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
     requestQueue.add(jsonObjectRequest);

Hope this will help you (Happy Coding !).