Volley with Kotlin using PHP and MySQL in Android Studio 3.0

Google officially announced Kotlin Support at Google I/O 2017. If you haven’t heard it yet, Kotlin is the brand new language for Android Application Development. It is developed by IntelliJ IDEA the same company that …

volley with kotlin

Google officially announced Kotlin Support at Google I/O 2017. If you haven’t heard it yet, Kotlin is the brand new language for Android Application Development. It is developed by IntelliJ IDEA the same company that has given the Android Studio IDE. Today in this post we will learn how we can perform networking using Volley with Kotlin.

If you want to learn Kotlin from starting then go through this playlist.

Before going further you should get the Android Studio 3.0 Canary 1. You can also add Kotlin Plugin to your existing Android Studio.

So lets start. Again we will start with building database and php script. This time I will do it little differently. As we have already seen it many times. Still for building secured RESTful APIs using SLIM Framework you can check this tutorial.

Creating Database

  • Open PhpMyAdmin and create the following table.

database

  • Thats it for the database now we will create the PHP project.

Creating Web Services

  • Now come inside htdocs folder (if you are using xampp) and create your project folder. Inside the project folder again create two folders named includes and v1.

php project

  • As you can see in the image I have my project directory named WebApi and inside that I have includes and v1.
  • Now come inside includes and create a new PHP file named Constants.php.

  • You should change the values accordingly here.

Connecting to Database

  • Now create one more file here named DbConnect.php and write the following code.

Performing Database Operation

  • Now we need another class to handle database operations. So create a file named DbOperation.php.

Handling API Calls

  • Now come inside v1 folder and create a file named index.php, here we will handle all the API calls.

Testing the API Calls

  • Again I am using POSTMAN to test the API.

post call get call

  • So as you can see the API is working fine. You can get the source code from the below link.

[sociallocker id=1372] Web Service Code Download [/sociallocker]

Making Android Project with Kotlin

  • Now create a new Android Studio project. Just remember you have to mark the Include Kotlin Support while creating the project.

kotlin project

  • Now inside activity_main.xml create the following UI.

android ui

  • You can use the following XML code for this.

  • For the spinner entries define the following array in your strings.xml file.

  • Now we will perform the Add operation using API Call.

Adding Volley to the Project

  • Come inside app level build.gradle file and add the following dependencies.

Creating a Singleton Class for Volley

  • If the application uses something frequently using a Single instance of that is a good idea. So here we will create a Singleton class for volley requests.
  • Inside your package create a Kotlin file named VolleySingleton.kt and write the following code.

  • Now we need to add the above class in the manifest.

  • As you can see I have also added the internet permission as it is required.

Creating and Object for holding EndPoints

  • Create a new Kotlin file named EndPoints.kt and write the following code.

  • You can see we are storing the API URLs here. Make sure you have changed the IP accordingly.

Adding Data to MySQL using Volley with Kotlin

  • Come inside MainActivity.kt and write the following code.

  • Now run the application and test.

adding artist

  • You can see we got the success message. You can check the database the record should be there.
  • Now we will try the second API call where we are fetching the values.

Fetching Data from MySQL using Volley with Kotlin

  • Create a new Activity named ViewArtistsActivity and in the layout file write the following code.

  • As you can see we have only a single list here to display all the artists in a list. Now we also need a custom layout file for the list.
  • So create a layout file named layout_list_artist.xml and write the following code.

  • Here we have only Two TextViews for name and genre.
  • Now we will define the model for the Artist.
  • So again create a new Kotlin class named Artist and write the following code.

  • Now we will define our custom adapter. So we need one more class. I have created a class named ArtistList.

  • Now come inside ViewArtistsActivity and write the following code.

  • Now try running the application again.

getting artist

  • Bingo! it is working fine. So this was volley with kotlin.
  • You can get the source code for this volley with kotlin example from below.

[sociallocker id=1372] Volley With Kotlin Source Code [/sociallocker]

So thats all for this Volley with Kotlin example friends. You may have realized that Kotlin code is lesser as compared to the equivalent Java code. So try practicing Kotlin and once you are used to it you will find it much easier than Java. We will post more about Kotlin from scratch. Till then, please SHARE this post if you found it useful. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x