Android Retrofit Tutorial to Insert into MySQL Database

In the last android retrofit tutorial I explained how to retrieve JSON using HTTP GET request. In this android retrofit tutorial we will use post request. So today we will insert some values from our android app to …

android retrofit tutorial

In the last android retrofit tutorial I explained how to retrieve JSON using HTTP GET request. In this android retrofit tutorial we will use post request.

So today we will insert some values from our android app to our MySQL Database.

In this tutorial I will be using Hostinger. Though you can use any hosting or local server (using wamp/xampp) as well.

So lets begin our android retrofit post request example. 🙂

Creating MySQL Database and PHP Scripts

  • As I told that I am using Hostinger. And this is the snapshot of my database.
mysql db
mysql db
  • This is my database, you should create the same database if you are going to copy my code. 😛
  • Now we will create a script to connect to our database. I have created dbConnect.php.

  • Our second script will receive and insert the data to mysql database. I have created insert.php.

  • Now upload both files to your hosting. And notedown the URL for your insert.php file. In my case it is

http://simplifiedcoding.16mb.com/RetrofitExample/insert.php

Actually we are going to use retrofit So in retrofit we keep the root URL and the Main URL separately. In the above URL the unbold part is our root URL. We will understand it in our Android Project. Lets create an Android Studio Project.

Android Retrofit Tutorial

  • Create a new Android Project. I have created project named RetrofitPostExample.
  • Add the following dependency to build.gradle file.

  • Add internet permission to your manifest.

  • Finally come to activity_main.xml. We have to create the following layout for this android retrofit tutorial.
android retrofit tutorial
android retrofit tutorial
  • Use the following xml code for the above layout.

  • Now come to MainActivity.java and write the following code.

  • For retrofit we have to create an Interface. So create an Interface named RegisterAPI and write the following code.

  • So as you can see this is a common java interface. Inside it we are having
    • @FormUrlEncoded, we have to write this if we want to send post data to the server.
    • @POST, because we are using an HTTP Post request we have written this. Inside it we have the URL of the script that will be receiving the post request. Note that the URL is excluding the root URL. And we have defined the root URL  in our MainActivity.
    • public void insert() this is the method that will actually send the request. Inside this method we are having.
      •  @Field(“key”) String variable inside key we have to write what we have written inside $_POST[‘key’] in our script. And we have to specify it for all the values we are going to send.
      • Callback<Response> callback it is also inside the retrofit library. It will receive the output from the server.
  • But this is only an interface and the method is abstract. We will define the method inside insertUser() method that is declared inside MainActivity.java.
  • So come to method insertUser() inside MainActivity.java

  • Now just run your application.
android retrofit tutorial
android retrofit tutorial
  • And if you just got the success message as shown in the above image. Check your database.
mysql db
mysql db
  • Bingo! It is working absolutely fine. If you are having troubles then you can download my php scripts and android project from below.

Get the Source Code of this Android Retrofit Tutorial

[sociallocker id=1372] [download id=”1557″] [/sociallocker]

Here are few more tutorials for android application development you should check 

So thats all for this android retrofit tutorial guys. Feel free to ask anything regarding this android retrofit tutorial by your comments. And don’t forget to share this tutorial 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