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

Volley Send Body Request Parameters

Kepala Suku

Web & Mobile Developer
Staff member
#1
Inisiasi variabel session, request queue, dan token:
Java:
String token = "Bearer ";
Sessions session;
RequestQueue queue;
Isi variable yang telah dideklarasikan:
Java:
queue = Volley.newRequestQueue(this);
session = new Sessions(this);
token += session.getSession(Variables.SESSION_TOKEN);
Buat request menggunakan Volley:
Java:
StringRequest request = new StringRequest(POST, Variables.URL_PACKAGE_ORDER, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject obj = new JSONObject(response);
                    if (!obj.isNull("success") && obj.getBoolean("success") == true) {
                        alert();
                        myDialog.dismiss();
                    } else {
                        showMessage("Gagal mengambil data");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error.networkResponse != null && error.networkResponse.statusCode == 401) {

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

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("package_id", package_id);
                return params;
            }
        };

        queue.add(request);
 

Kepala Suku

Web & Mobile Developer
Staff member
#2
Fungsi showMessage
Java:
private void showMessage(String msg) {
  Snackbar.make(txtnama, msg, Snackbar.LENGTH_LONG).show();
}