Wizard Book


Programming Python, Web, Android

Android: Mengakses Login API, Mendapatkan JWT (JSON Web Token) menggunakan Volley

Android Mengakses Login API Menggunakan Library Volley untuk Mendapatkan 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 buttonLogin;
    String your_JWT_Token = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonLogin = (Button)findViewById(R.id.buttonLogin);
                buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JSONObject params = null;
                params = new JSONObject();
                try {
                    params.put("email", "kaima@email.com");
                    params.put("password", "123456789");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                cek_login(params);
            }
        });

    }

    public void cek_login(JSONObject data){
        RequestQueue requstQueue = Volley.newRequestQueue(this);

        JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, "http://192.168.43.213/php-project/api/login.php",data,
                new Response.Listener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                        try {
                            //JSONObject jsonobject = jsonarray.getJSONObject(i);
                            String message = response.getString("message");
                            String jwt = response.getString("jwt");
                            your_JWT_Token = jwt;
                            Toast.makeText(MainActivity.this, your_JWT_Token, Toast.LENGTH_SHORT).show();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
        );
        requstQueue.add(jsonobj);
    }

}

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