• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Catatan Send Post Body Using Http Authentication Basic On Android Studio

Web & Mobile Developer
Staff member
This is full code used for send post body request using Volley library.
Java:
StringRequest request = new StringRequest(POST, Variables.URL_GETJOB, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.d("respon", response);
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            if (error.networkResponse.statusCode == 401) {

                            }
                        }
                    }
                    ) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> reqMap = new LinkedHashMap<>();
                            //here is the post paameter should be written
                            reqMap.put("name", "Ahmad Budairi");

                            return reqMap;
                        }

                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            HashMap<String, String> params = new HashMap<String, String>();
                            String creds = String.format("%s:%s", YOUR_USERNAME_STRING, YOUR_PASSWORD_STRING);
                            String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                            params.put("Authorization", auth);
                            return params;
                        }


                    };
                    RequestQueue queue = Volley.newRequestQueue(GetJob.this);
                    queue.add(request);