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 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.

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.

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 🙂

18 thoughts on “Volley with Kotlin using PHP and MySQL in Android Studio 3.0”

  1. Hello I try your code and it works almost perfect ! However; I have a problem to write in my dataBase, I try to use POSTMAN and with POSTMAN it works perfectly so I guess it comes from the Kotlin code and particularly this part of code :

    val stringRequest = object : StringRequest(Request.Method.POST, EndPoints.URL_ADD_ARTIST,

    Response.Listener { response ->
    try {
    val obj = JSONObject(response)
    Toast.makeText(applicationContext, obj.getString(“message”), Toast.LENGTH_LONG).show()
    } catch (e: JSONException) {
    e.printStackTrace()
    }
    },
    object : Response.ErrorListener {
    override fun onErrorResponse(volleyError: VolleyError) {
    Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show()
    }
    }) {
    @Throws(AuthFailureError::class)
    override fun getParams(): Map {
    val params = HashMap()
    params.put(“name”, name)
    params.put(“genre”, genre)
    return params
    }
    }

    My problem is this when I click on the button “ADD ARTIST” I have nothing it is like if there is nothing. Someone have an idea to solve this ?

    Thank you again ! 🙂

    Reply
  2. Hallo Belal,

    in volley with java, I could put headers.
    in kotlin ? how to do that ?

    java :
    @Override
    public Map getHeaders() throws AuthFailureError {
    HashMap headers = new HashMap();
    String auth = Config.X_TOKEN;
    headers.put(“x-token”, auth);
    return headers;
    }

    kotlin ??

    Reply
  3. Hey buddy, I need your help like every emergently, I have searched the interenet like all. This app
    is so cool. Please tell me how to delete and update too

    Reply
  4. Hi Belal
    Thanks for the videos so far, keep up the good work!
    I’m trying to build, however I get this error, any ideas?
    …Type inference failed: Not enough information to infer parameter T in fun findViewById(p0: Int): T!…
    Also can’t the find the video to this example neither the code, can you help?
    Thanks

    Reply
  5. Hi Belal,
    Great Tutorial, everything seems to work fine.
    Do you know if I can use the same method of viewing the artist list but within a fragment?
    I have changed the ArtistList.kt file to reference a Fragment but I am getting an error on the ArrayAdapter having the wrong context…
    Any ideas?
    Thanks, Jon

    Reply

Leave a Comment