Tutorial CRUD Rest API Menggunakan Retrofit 2 Android

Published by farizdotid on

Tutorial CRUD Rest API Menggunakan Retrofit 2 Android

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/farizdot/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Tutorial CRUD Rest API Menggunakan Retrofit 2 Android – Sebelum memulai project ini alangkah lebih baiknya kita mempunyai atau membuat terlebih dahulu Rest API untuk kebutuhan CRUD menggunakan retrofit 2 ini. Untuk cara membuat rest apinya agan bisa ke artikel Cara Membuat API CRUD Menggunakan Slim Framework. Jika Rest API sudah dibuat maka langkah selanjutnya agan bisa mulai dengan project androidnya. Project android disini kita lanjutkan dari project sebelumnya ya, nama projectnya “Mahasiswa App”. Mahasiswa App ini mempunyai fitur untuk melihat list dosen, tambah matkul, matkul apa saja yang sudah mahasiswa ambil dan menghapus mata kuliah yang sudah diambil oleh mahasiswa.

Tutorial CRUD Rest API Menggunakan Retrofit 2 Android

Tutorial CRUD Rest API Menggunakan Retrofit 2 Android

Jadi, didalam tutorial crud rest api menggunakan retrofit 2 ini agan akan belajar mengenai cara GET, POST, dan Delete menggunakan retrofit 2.  Project Mahasiswa sebelumnya agan bisa dilihat di artikel : Tutorial Login dan Register Menggunakan Retrofit2 dan API Server.

 

Persiapan :

Rest API yang sudah siap :

Tambahkan library retrofit 2 dan gson converter terlebih dahulu :

 

Pertama,

Buat file model bernama ResponseDosen dan isi dengan kode berikut :

dan buat file bernama SemuadosenItem dengan kode berikut :

Catatan :

Model tersebut disesuaikan dengan hasil json yang dihasilkan oleh API GET Semua Dosen.

 

Kedua,

Buka file BaseApiService lalu tambahkan kode berikut :

 

Ketiga,

Tambahkan activity baru bernama DosenActivity. Di dosen activity ini berisi list-list dosen. Jika sudah terbuat buka activity_dosen.xml lalu tambahkan kode berikut :

Catatan :

Jangan lupa tambahkan library RecyclerView dan Cardview kedalam project agan ya. Jika belum agan bisa menambahkan code berikut di file gradle.app :

 

Keempat,

Buatlah file layout xml bernama item_dosen. Lalu masukan kode berikut :

 

Kelima,

Buatlah file bernama DosenAdapter.java. Lalu isikan dengan kode berikut :

Catatan :

  • Jangan lupa untuk meng-extend class dengan RecyclerView.Adapter<DosenAdapter.DosenHolder> ya.
  • Jika ada error dibagian TextDrawable agan tinggal tambahkan library baru kedalam gradle.app. Tambahkan kode berikut :

 

Keenam,

Buka DosenActivity.java. Lalu tambahkan kode berikut tepat diatas onCreate :

Lalu didalam onCreate tambahkan kode berikut :

Jika ada error dibagian getResultListDosen() silahkan tambahkan kode berikut :

 

Ketujuh,

Buatlah activity baru bernama MatkulActivity dan buka activity_matkul.xml lalu masukan kode berikut :

 

Kedelapan,

Buatlah file layout xml baru bernama item_matkul. Lalu masukan kode berikut :

 

Kesembilan,

Buat class model baru bernama ResponseMatkul dan tambahkan kode berikut :

dan buat kembali kelas model dengan nama SemuamatkulItem lalu masukan kode berikut :

Catatan :

Model tersebut disesuaikan dengan hasil json yang dihasilkan oleh API GET Semua Dosen.

 

Kesepuluh,

Buka file BaseApiService lalu tambahkan kode berikut :

 

Kesebelas,

Buatlah file baru bernama MatkulAdapter lalu tambahkan kode berikut :

Catatan :

  • Jangan lupa untuk meng-extend class dengan RecyclerView.Adapter<MatkulAdapter.MatkulHolder> ya

 

Keduabelas,

Buka file MatkulActivity lalu tambahkan kode berikut tepat diatas onCreate :

Lalu didalam onCreate tambahkan kode berikut :

 

Jika getDataMatkul() merah buat method bernama getDatamatkul() lalu isi dengan kode berikut :

Lalu buatlah fungsi kembali bernama initDataIntent. Fungsi ini berguna untuk mengirim data matkul activity ke detail_matkul activity. Masukan kode berikut :

Catatan :

  • Jika RecyclerItemClickListener masih merah silahkan agan menuju artikel saya untuk membuat file interface recycler view tersebut. Agan bisa cek disini : Recycler View Click Listener
  • Jika MatkulDetailActivity masih merah biarkan saja terlebih dahulu karena kita belum membuat activity tersebut.
  • Jika TambahMatkulActivity masih merah biarkan saja terlbih dahulu karea kita belum membuat activity tersebut.

 

Ketigabelas,

Buatlah activity baru bernama MatkulDetailActivity lalu buka activity_matkul_detail dan isi dengan kode berikut :

Lalu di class MatkulDetailActivity masukan kode berikut tepat diatas onCreate :

Lalu didalam onCreate tambahkan kode berikut :

 

Keempatbelas,

Didalam class MatkulDetailActivity tambahkan fungsi bernama requestDeleteMatkul() lalu tambahkan kode berikut :

Catatan :

Jika kode didalam fungsi tersebut masih error itu dikarenakan agan belum buat interface delete didalam class baseapiservice. Silahkan agan buka terlebih dahulu class baseapiservicenya lalu tambahkan kode berikut :

ID matkul disini didapatkan dari ketika user mengklik suatu list di activity sebelumnya.

 

Kelimabelas,

Buat activity baru bernama TambahMatkulActivity lalu buka activity_tambah_matkul lalu masukan kode berikut :

 

Keenambelas,

Buka file BaseApiService lalu tambakan kode berikut :

 

Ketujuhbelas,

Buka TambahMatkulActivity lalu tambahkan kode berikut :

Lalu didalam onCreate tambahkan kode berikut :

Didalam activity tersebut tambahkan fungsi bernama initSpinnerDosen() tambahkan kode berikut :

Lalu tambahkan fungsi requestDetailDosen dengan parameter String namadosen dan masukan kode berikut :

Lalu tambahkan fungsi kembali dengan nama requestSimpanMatkul. Fungsi ini berguna untuk menambahkan kedalam database. Untuk kodenya masukan dengan kode berikut :

Kedelapanbelas,

Sampai sini silahkan jalankan aplikasi dan semoga lancar.

 

Untuk screenshot aplikasinya begini :

    

 

Download Project Tutorial CRUD Rest API Menggunakan Retrofit 2 Android ( Mahasiswa App ) :

[sociallocker]

Download Mahasiswa App

[/sociallocker]

 

Sampai disini agan sudah memakai Retrofit2 dengan method POST,GET, dan DELETE. Method POSTnya disini disaat kasus mahasiswa menambahkan mata kuliah, Method GET disaat ambil list-list dosen, Method DELETE pada kasus mengapus mata kuliah. Sekian artikel mengenai Tutorial CRUD Rest API Menggunakan Retrofit 2 Android. Kurang lebihnya saya mohon maaf, Ditunggu komentar mengenai artikel ini. Semoga bermanfaat.


28 Comments

Cadis · August 9, 2017 at 1:12 am

Ini gak pake ip localhost mas? saya lihat tutorialnya gak ada

    farizdotid · August 9, 2017 at 5:55 am

    sama saja gan konsepnya. bedanya hanya di url aja disesuaikan.

kopianan · August 11, 2017 at 6:18 am

Gannn…
saya baru liat sekilas dan saya mau tanya apakah ada versi video nya ? dan sekalian mau tanya jika saya curi ilmu untuk dijadikan versi youtube (vidio) boleh gak gan ?

    farizdotid · August 11, 2017 at 8:21 am

    Belum ada gan. Silahkan gan, cantumkan sumber aja jangan lupa untuk saling menghargai 🙂

Muhammad Imanudin · August 12, 2017 at 10:54 am

Sangat membantu

Bervianto Leo · August 13, 2017 at 1:19 am

Mantap. Bagus tutorialnya, mungkin bisa disandingkan dengan tutorial menggunakan realm. Bisa juga sama design pattern.

    farizdotid · August 13, 2017 at 2:18 am

    Terimakasih gan sarannya. Untuk kedepannya insyaAllah saya buat seperti itu gan.

tati · August 14, 2017 at 5:31 am

gan, minta contoh php yg get matkul atau dosen tp berdasarkan id….boleh gak gan?

Ribfan · December 3, 2017 at 2:37 pm

gan saya mau tanya kok saya saat klik tombol lihat matkul appnya force close terus ya, padahal sudah ikutin step by step

Dudi · May 10, 2019 at 10:57 am

Gan ini menggunakan JSON ga ya?

    farizdotid · May 10, 2019 at 3:37 pm

    Ada kok gan, Response jsonnya bisa ditangkep dari sini : response.body().getSemuadosen();

Dicky · November 13, 2019 at 6:26 pm

Gan cara buat 2 listview gimana ya, pada aplikasi saya kan terdapat dua actor, admin dan user. Pada actor admin, setOnClickListener nya saya arahkan ke update data. Lalu untuk actor user saya mau arahkan ke detail data. Untuk bagian admin saya sudah buat, tinggal user saja. Kira-kira adakah caranya? Makasih

    Dicky · November 13, 2019 at 6:33 pm

    Maksud saya apakah bisa buat dua adapter dalam satu aplikasi. Atau ada metode lain untuk actor usernya. Soalnya ketika saya buat adapter lagi buat user terdapat error pada onCreateViewHolder nya

dewi · November 18, 2019 at 9:34 am

gan ini database udah aku masukin ke loclahost, tapi kok gak bisa register? ip sudah saya ganti tapi tetap gak bisa register

    farizdotid · November 22, 2019 at 9:40 pm

    Ada pesan error atau screenshot pada saat gabisa register ?

Jaka · November 27, 2019 at 1:06 pm

kakak ada tutuorial yang gradle nya 3.5 ga ? aku coba yang di github ga bisa thakns

    Jaka · November 27, 2019 at 1:10 pm

    o ia sama list view nya yang pake data pagination gimana ya kakak ?

Jaka · November 27, 2019 at 1:54 pm

kak request tutorial yang kaya gini lagi tapi versi android studio terbaru dong plissss

Jaka · November 29, 2019 at 9:05 pm

kak buat yang versi androidx nya dong, lagi belajar ni

Iqbal Frzl · December 4, 2019 at 12:55 pm

Ini kalo beackend nya selain pake slim, pake laravel misalnya. bisa kah?

    farizdotid · December 5, 2019 at 2:08 pm

    Bisa gan. Ga masalah framework APInya dibuat darimana yang penting kita tau API dan kebutuhan yang dibutuhkan oleh API saja.

Cara Set Data Spinner dari Json API Server ( Retrofit ) Android · August 12, 2017 at 11:38 am

[…] detail dari project sebelumnya yaitu Mahasiswa App, Kalian bisa lihat di artikel sebelumnya di Tutorial CRUD Rest API Menggunakan Retrofit 2 Android . Bedanya di project tersebut saya tidak menjelaskan bagaimana cara set data spinner dari json api […]

Cara Membuat Bottom Navigation Dengan Fragment Android · September 22, 2017 at 2:22 pm

[…] Tutorial CRUD Rest API Menggunakan Retrofit 2 Android […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.