Wizard Book


Programming Python, Web, Android

Android: Mengakses Protected Get Data API Menggunakan Volley dengan Header JWT

Android Mengakses Protected Get Data API Menggunakan Library Volley dengan Header JWT (JSON Web Token)

// app/build.gradle => implementation 'com.android.volley:volley:1.1.1'
// AndroidManifest.xml => 
// AndroidManifest.xml => android:usesCleartextTraffic="true"

package com.contohprogram.jwt_authentication_login_cek;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private JSONObject jObject;
    private String jsonResult ="";
    ProgressDialog pd;
    Button buttonGetProtected;
    String your_JWT_Token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJib3NzIiwiYXVkIjoicmFreWF0IiwiaWF0IjoxNTYwMzUyMTI1LCJuYmYiOjE1NjAzNTIxMzUsImV4cCI6MTU2MDM1MjcyNSwiZGF0YSI6eyJpZCI6IjEiLCJmaXJzdG5hbWUiOiJrYWltYSIsImxhc3RuYW1lIjoiYWJiZXMiLCJlbWFpbCI6ImthaW1hQGVtYWlsLmNvbSJ9fQ.A7TG6SLuSClE3wTCsoplDrEy-nJPymxtsard_3co3zc";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonGetProtected = (Button)findViewById(R.id.buttonGetProtected);
        buttonGetProtected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                get_protected_data();
            }
        });
    }

    public void get_protected_data() {

        StringRequest PostRequest = new StringRequest(Request.Method.POST, "http://192.168.43.213/php-project/api/protected.php", new Response.Listener() {
            @Override
            public void onResponse(String response) {
                try {
                    Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(getBaseContext(), "Gagal",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

                pd.dismiss();
            }

        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                    }
                }
        ){
            protected Map getParams(){
                Map params = new HashMap<>();

                //params.put("username", User); //not used
                //params.put("password", Pass); //not used

                return params;
            }
            @Override
            public Map getHeaders() throws AuthFailureError {
                Map params = new HashMap();
                params.put("Authorization", "Bearer "+ your_JWT_Token);
                return params;
            }
        };

        pd = ProgressDialog.show(MainActivity.this, "Please Wait", "Connecting", true);
        pd.setCancelable(true);

        Volley.newRequestQueue(this).add(PostRequest);
    }
}

email : contohprogram.com@gmail.com
WA : +6289671400363