Hello friends, Here is another Android MySQL Tutorial, in this post we will learn the basic CRUD operation in MySQL database from Android Application. So without wasting time lets start our Android MySQL Tutorial.
Table of Contents
What is CRUD?
I guess many of you already know that what is CRUD. But if anyone don’t know, CRUD is an Acronym for the Basic Database Operations. In any database we do the following basic operations.
- Creating a Record -> In the database we insert a record.
- Reading Stored Records -> No point of saving data when we can’t read it back 😛 (LOL). So the second operation is reading the stored data back.
- Updating Stored Records -> We may also need to update the existing data. So the third operation is update.
- Deleting Records -> Lastly we may also need to delete the existing data from the database.
So basically in any database we perform the above mentioned operations. In this tutorial I am going to use MySQL and PHP.
Why PHP and MySQL and Why Not SQLite?
Android gives us a feature of SQLite to use as the RDBMS for our app. But we can’t use SQLite only if we are building an app. This is because we need a centralize server. SQLite is local for every device so if a user is using your application you will not be able manage your users if it is only in SQLite. That is why we need a centralize database where we can store our app data.
If you want to learn about the SQLite using the Latest Architecture Component ROOM. Then here is a complete FREE COURSE for Android Room Database.
Do we have only PHP and MySQL for this?
Obviously NO? You can use any server side scripting language or database application. I am using here PHP and MySQL because it is easily available and I already know it. 😛 (LOL) But if you are an expert in Python or JAVA or NodeJS or basically any other technology then you can go with it. It will change only the server side coding. Android part will always be the same.
Building Web APIs
The first step is building the required Web APIs. This is because from the android application, to communicate with our web server we need an interface called API.
So our android device will send a request to our API, then our API will perform the requested task and it will give us the response related to the task. You can see the below diagram for more clarification.
The response that the web server gives should be in a standard structure, so that in our android side we can easily parse and get the information from the structure. For this everyone now uses JSON. If you heard this term for the first time then you should consider watching this 7 minute quick JSON tutorial.
I guess you got a basic idea about the task that we are going to do in this tutorial.
Creating Database
It is obvious that we need a database first 😛 . So here I am using XAMPP (You can go with wamp or lamp as well). So first create the following database.
- So open localhost/phpmyadmin and run the following query to create the above table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -- created by Belal Khan -- website: www.simplifiedcoding.net -- tables -- Table: heroes CREATE TABLE heroes ( id int NOT NULL AUTO_INCREMENT, name varchar(200) NOT NULL, realname varchar(200) NOT NULL, rating int NOT NULL, teamaffiliation varchar(100) NOT NULL, CONSTRAINT heroes_pk PRIMARY KEY (id) ); -- End of file. |
Creating PHP Project
Now to perform the Database Operations we will create a PHP Project.
- So inside htdocs (c:/xampp/htdocs) create a new folder (You can also use an IDE like PHP Storm to create project but remember create the project inside c:/xampp/htdocs only).
- I have given the name HeroApi to my project. You can give any name. But I would say give the same name or else you may lead up to some errors following the post if you are a newbie.
- Inside the project create two more folders named includes and Api. You can see the below screenshot for the directory structure that I am using. (I am using Sublime Text for coding server part).
- You see we have four php files (3 inside includes and 1 inside v1). So you create these files as well.
Project Structure
- So we have the following things in our PHP project.
- includes
- Constants.php: In this file we will define all the required constants e.g., database name, username, password etc.
- DbConnect.php: This fill will contain a class where we will connect to our MySQL database.
- DbOperation.php: The actual CRUD operation is performed inside this file.
- v1
- Api.php: This is our API, we will send request to this file only from the android side. And this file will handle all the API calls.
- includes
Defining Constants
- First come inside the file Constants.php and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php /* * Created by Belal Khan * website: www.simplifiedcoding.net */ define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASS', ''); define('DB_NAME', 'android'); |
Connecting to Database
- Now inside DbConnect.php write the following code. I have explained the code using comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php /* * Created by Belal Khan * website: www.simplifiedcoding.net */ //Class DbConnect class DbConnect { //Variable to store database link private $con; //Class constructor function __construct() { } //This method will connect to the database function connect() { //Including the constants.php file to get the database constants include_once dirname(__FILE__) . '/Constants.php'; //connecting to mysql database $this->con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); //Checking if any error occured while connecting if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //finally returning the connection link return $this->con; } } |
Performing Database Operations
- Now we will do the CRUD operation inside the DbOperation.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | <?php class DbOperation { //Database connection link private $con; //Class constructor function __construct() { //Getting the DbConnect.php file require_once dirname(__FILE__) . '/DbConnect.php'; //Creating a DbConnect object to connect to the database $db = new DbConnect(); //Initializing our connection link of this class //by calling the method connect of DbConnect class $this->con = $db->connect(); } /* * The create operation * When this method is called a new record is created in the database */ function createHero($name, $realname, $rating, $teamaffiliation){ $stmt = $this->con->prepare("INSERT INTO heroes (name, realname, rating, teamaffiliation) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssis", $name, $realname, $rating, $teamaffiliation); if($stmt->execute()) return true; return false; } /* * The read operation * When this method is called it is returning all the existing record of the database */ function getHeroes(){ $stmt = $this->con->prepare("SELECT id, name, realname, rating, teamaffiliation FROM heroes"); $stmt->execute(); $stmt->bind_result($id, $name, $realname, $rating, $teamaffiliation); $heroes = array(); while($stmt->fetch()){ $hero = array(); $hero['id'] = $id; $hero['name'] = $name; $hero['realname'] = $realname; $hero['rating'] = $rating; $hero['teamaffiliation'] = $teamaffiliation; array_push($heroes, $hero); } return $heroes; } /* * The update operation * When this method is called the record with the given id is updated with the new given values */ function updateHero($id, $name, $realname, $rating, $teamaffiliation){ $stmt = $this->con->prepare("UPDATE heroes SET name = ?, realname = ?, rating = ?, teamaffiliation = ? WHERE id = ?"); $stmt->bind_param("ssisi", $name, $realname, $rating, $teamaffiliation, $id); if($stmt->execute()) return true; return false; } /* * The delete operation * When this method is called record is deleted for the given id */ function deleteHero($id){ $stmt = $this->con->prepare("DELETE FROM heroes WHERE id = ? "); $stmt->bind_param("i", $id); if($stmt->execute()) return true; return false; } } |
Handling API Calls
- Now here come the main part, which is handling the API calls. So come inside the Api.php which is inside the v1 folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | <?php //getting the dboperation class require_once '../includes/DbOperation.php'; //function validating all the paramters are available //we will pass the required parameters to this function function isTheseParametersAvailable($params){ //assuming all parameters are available $available = true; $missingparams = ""; foreach($params as $param){ if(!isset($_POST[$param]) || strlen($_POST[$param])<=0){ $available = false; $missingparams = $missingparams . ", " . $param; } } //if parameters are missing if(!$available){ $response = array(); $response['error'] = true; $response['message'] = 'Parameters ' . substr($missingparams, 1, strlen($missingparams)) . ' missing'; //displaying error echo json_encode($response); //stopping further execution die(); } } //an array to display response $response = array(); //if it is an api call //that means a get parameter named api call is set in the URL //and with this parameter we are concluding that it is an api call if(isset($_GET['apicall'])){ switch($_GET['apicall']){ //the CREATE operation //if the api call value is 'createhero' //we will create a record in the database case 'createhero': //first check the parameters required for this request are available or not isTheseParametersAvailable(array('name','realname','rating','teamaffiliation')); //creating a new dboperation object $db = new DbOperation(); //creating a new record in the database $result = $db->createHero( $_POST['name'], $_POST['realname'], $_POST['rating'], $_POST['teamaffiliation'] ); //if the record is created adding success to response if($result){ //record is created means there is no error $response['error'] = false; //in message we have a success message $response['message'] = 'Hero addedd successfully'; //and we are getting all the heroes from the database in the response $response['heroes'] = $db->getHeroes(); }else{ //if record is not added that means there is an error $response['error'] = true; //and we have the error message $response['message'] = 'Some error occurred please try again'; } break; //the READ operation //if the call is getheroes case 'getheroes': $db = new DbOperation(); $response['error'] = false; $response['message'] = 'Request successfully completed'; $response['heroes'] = $db->getHeroes(); break; //the UPDATE operation case 'updatehero': isTheseParametersAvailable(array('id','name','realname','rating','teamaffiliation')); $db = new DbOperation(); $result = $db->updateHero( $_POST['id'], $_POST['name'], $_POST['realname'], $_POST['rating'], $_POST['teamaffiliation'] ); if($result){ $response['error'] = false; $response['message'] = 'Hero updated successfully'; $response['heroes'] = $db->getHeroes(); }else{ $response['error'] = true; $response['message'] = 'Some error occurred please try again'; } break; //the delete operation case 'deletehero': //for the delete operation we are getting a GET parameter from the url having the id of the record to be deleted if(isset($_GET['id'])){ $db = new DbOperation(); if($db->deleteHero($_GET['id'])){ $response['error'] = false; $response['message'] = 'Hero deleted successfully'; $response['heroes'] = $db->getHeroes(); }else{ $response['error'] = true; $response['message'] = 'Some error occurred please try again'; } }else{ $response['error'] = true; $response['message'] = 'Nothing to delete, provide an id please'; } break; } }else{ //if it is not api call //pushing appropriate values to response array $response['error'] = true; $response['message'] = 'Invalid API Call'; } //displaying the response in json structure echo json_encode($response); |
- Now lets test the APIs.
Testing the API Calls
- For testing the API Calls here I am using POSTMAN. It is a REST API Client for Google Chrome.
Create Operation
Read Operation
Update Operation
Delete Operation
You see all the operations are working absolutely fine. Now we can move ahead in creating our Android Project. But before moving to android side below you can see our API URLs.
Finalizing API Calls
- The below table displays our API URLs with the Parameter and Method. Remember using localhost in android side will not work. You need to find your IP. I have my IP in the below table, but in your case you need to find yours. So if you are using a windows you can use ipconfig command to find IP and for MAC or Linux use the ifconfig command. For more details you can visit this tutorial.
- POST: http://192.168.101.1/HeroApi/v1/Api.php?apicall=createhero
- GET: http://192.168.101.1/HeroApi/v1/Api.php?apicall=getheroes
- POST: http://192.168.101.1/HeroApi/v1/Api.php?apicall=updatehero
- GET: http://192.168.101.1/HeroApi/v1/Api.php?apicall=deletehero&id=idvalue
Want to Explore Building RESTful APIs?
Here we built a very basic API that demonstrates the CRUD operation. But in real world scenarios we use FRAMEWORKS to make the API creating easy and structured. If you want to dig a bit more about building RESTful APIs. Then you should check the following course. It is absolutely FREE.
Android MySQL Tutorial
We have our Web Services, and now we can build the android application. So lets start.
Creating a new Project
- Create a new Android Studio Project. Here I have created a project named MyHeroApp.
- Once your project is loaded inside the package we will create all the helper classes that is required for thie project.
Creating Helper Classes
Class To Store API URLs
- First create a class named Api.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package net.simplifiedlearning.myheroapp; /** * Created by Belal on 9/9/2017. */ public class Api { private static final String ROOT_URL = "http://192.168.101.1/HeroApi/v1/Api.php?apicall="; public static final String URL_CREATE_HERO = ROOT_URL + "createhero"; public static final String URL_READ_HEROES = ROOT_URL + "getheroes"; public static final String URL_UPDATE_HERO = ROOT_URL + "updatehero"; public static final String URL_DELETE_HERO = ROOT_URL + "deletehero&id="; } |
Hero Model Class
- We also need a model class for our Hero. So create a class named Hero.java and write the following code. It will have only properties, constructor and getters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package net.simplifiedlearning.myheroapp; import java.io.Serializable; /** * Created by Belal on 9/9/2017. */ class Hero { private int id; private String name, realname; private int rating; private String teamaffiliation; public Hero(int id, String name, String realname, int rating, String teamaffiliation) { this.id = id; this.name = name; this.realname = realname; this.rating = rating; this.teamaffiliation = teamaffiliation; } public int getId() { return id; } public String getName() { return name; } public String getRealname() { return realname; } public int getRating() { return rating; } public String getTeamaffiliation() { return teamaffiliation; } } |
Request Handler
- We need to send GET and POST request to our API URLs, and for this I am creating a new class that will perform these tasks. So create a new class named RequestHandler.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | package net.simplifiedlearning.myheroapp; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import javax.net.ssl.HttpsURLConnection; /** * Created by Belal on 9/9/2017. */ public class RequestHandler { //Method to send httpPostRequest //This method is taking two arguments //First argument is the URL of the script to which we will send the request //Other is an HashMap with name value pairs containing the data to be send with the request public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams) { //Creating a URL URL url; //StringBuilder object to store the message retrieved from the server StringBuilder sb = new StringBuilder(); try { //Initializing Url url = new URL(requestURL); //Creating an httmlurl connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //Configuring connection properties conn.setReadTimeout(15000); conn.setConnectTimeout(15000); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); //Creating an output stream OutputStream os = conn.getOutputStream(); //Writing parameters to the request //We are using a method getPostDataString which is defined below BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write(getPostDataString(postDataParams)); writer.flush(); writer.close(); os.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpsURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); sb = new StringBuilder(); String response; //Reading server response while ((response = br.readLine()) != null) { sb.append(response); } } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } public String sendGetRequest(String requestURL) { StringBuilder sb = new StringBuilder(); try { URL url = new URL(requestURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); String s; while ((s = bufferedReader.readLine()) != null) { sb.append(s + "\n"); } } catch (Exception e) { } return sb.toString(); } private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); boolean first = true; for (Map.Entry<String, String> entry : params.entrySet()) { if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } return result.toString(); } } |
Defining Internet Permission in AndroidManifest
- As we need to perform network request from our Application, we need to define internet permission for this. And because we are working with localhost our APIs are not secured i.e. we have HTTP URLs and not HTTPS for our APIs.
- And by default your application are restricted to communicate with non HTTPS URLs (For security reasons). So you need to explicitly define that your app should be allowed to communicate with HTTP URLs. And to do this you need to add usesCleartextTraffic=”true” inside your opening application tag. (See the below code that is AndroidManifest.xml for my project.).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.simplifiedlearning.myheroapp"> <!-- this is the internet permission --> <uses-permission android:name="android.permission.INTERNET" /> <application android:usesCleartextTraffic="true" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Designing User Interface
- The below image shows how our final application will look.

- So we need to design the above mentioned things. I have already designed the activity_main.xml so you can directly use the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="net.simplifiedlearning.myheroapp.MainActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/editTextHeroId" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" /> <EditText android:id="@+id/editTextName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:hint="Name" /> <EditText android:id="@+id/editTextRealname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:hint="Realname" /> <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="1" /> <Spinner android:id="@+id/spinnerTeamAffiliation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:entries="@array/teams" /> <Button android:id="@+id/buttonAddUpdate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add" /> </LinearLayout> <ListView android:id="@+id/listViewHeroes" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/linearLayout" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:visibility="gone" /> </RelativeLayout> |
- For the spinner I have used static entries using xml array. So you also need to create this array. For this go inside values -> strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 | <resources> <string name="app_name">Android MySQL CRUD</string> <!-- defining the array for our spinner --> <array name="teams"> <item>Avengers</item> <item>Justice League</item> <item>X-Men</item> <item>Fantastic Four</item> </array> </resources> |
- Thats it for the User Interface part.
- Now lets perform the CRUD operations in our MySQL database.
Class to Perform Network Request
- The point is we cannot directly perform a network request in the application’s main thread. So for this we need an AsyncTask to perform the task in a separate thread. Hence we will create an inner class inside MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | //inner class to perform network request extending an AsyncTask private class PerformNetworkRequest extends AsyncTask<Void, Void, String> { //the url where we need to send the request String url; //the parameters HashMap<String, String> params; //the request code to define whether it is a GET or POST int requestCode; //constructor to initialize values PerformNetworkRequest(String url, HashMap<String, String> params, int requestCode) { this.url = url; this.params = params; this.requestCode = requestCode; } //when the task started displaying a progressbar @Override protected void onPreExecute() { super.onPreExecute(); progressBar.setVisibility(View.VISIBLE); } //this method will give the response from the request @Override protected void onPostExecute(String s) { super.onPostExecute(s); progressBar.setVisibility(GONE); try { JSONObject object = new JSONObject(s); if (!object.getBoolean("error")) { Toast.makeText(getApplicationContext(), object.getString("message"), Toast.LENGTH_SHORT).show(); //refreshing the herolist after every operation //so we get an updated list //we will create this method right now it is commented //because we haven't created it yet //refreshHeroList(object.getJSONArray("heroes")); } } catch (JSONException e) { e.printStackTrace(); } } //the network operation will be performed in background @Override protected String doInBackground(Void... voids) { RequestHandler requestHandler = new RequestHandler(); if (requestCode == CODE_POST_REQUEST) return requestHandler.sendPostRequest(url, params); if (requestCode == CODE_GET_REQUEST) return requestHandler.sendGetRequest(url); return null; } } |
Create Operation
- Now lets save a new hero to our database. First we will define all the views. And we will attach a clicklistener to the button and inside the click event we will call the method to create a new record in the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | public class MainActivity extends AppCompatActivity { private static final int CODE_GET_REQUEST = 1024; private static final int CODE_POST_REQUEST = 1025; //defining views EditText editTextHeroId, editTextName, editTextRealname; RatingBar ratingBar; Spinner spinnerTeam; ProgressBar progressBar; ListView listView; Button buttonAddUpdate; //we will use this list to display hero in listview List<Hero> heroList; //as the same button is used for create and update //we need to track whether it is an update or create operation //for this we have this boolean boolean isUpdating = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextHeroId = (EditText) findViewById(R.id.editTextHeroId); editTextName = (EditText) findViewById(R.id.editTextName); editTextRealname = (EditText) findViewById(R.id.editTextRealname); ratingBar = (RatingBar) findViewById(R.id.ratingBar); spinnerTeam = (Spinner) findViewById(R.id.spinnerTeamAffiliation); buttonAddUpdate = (Button) findViewById(R.id.buttonAddUpdate); progressBar = (ProgressBar) findViewById(R.id.progressBar); listView = (ListView) findViewById(R.id.listViewHeroes); heroList = new ArrayList<>(); buttonAddUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //if it is updating if (isUpdating) { //calling the method update hero //method is commented becuase it is not yet created //updateHero(); } else { //if it is not updating //that means it is creating //so calling the method create hero createHero(); } } }); //calling the method read heroes to read existing heros from the database //method is commented because it is not yet created //readHeroes(); } |
- Now we need to create the method createHero().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | private void createHero() { String name = editTextName.getText().toString().trim(); String realname = editTextRealname.getText().toString().trim(); int rating = (int) ratingBar.getRating(); String team = spinnerTeam.getSelectedItem().toString(); //validating the inputs if (TextUtils.isEmpty(name)) { editTextName.setError("Please enter name"); editTextName.requestFocus(); return; } if (TextUtils.isEmpty(realname)) { editTextRealname.setError("Please enter real name"); editTextRealname.requestFocus(); return; } //if validation passes HashMap<String, String> params = new HashMap<>(); params.put("name", name); params.put("realname", realname); params.put("rating", String.valueOf(rating)); params.put("teamaffiliation", team); //Calling the create hero API PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_CREATE_HERO, params, CODE_POST_REQUEST); request.execute(); } |
- Now run the application and try adding a new hero.

- Its working fine, you can check the database as well. Now lets move to the read operation.
Read Operation
- We will display all the heroes from the database in a ListView, the ListView also have the Update and Delete Button.
- So for this first we will create a custom Layout for our ListView.
List Layout
- Create a layout resource file named layout_hero_list.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="8dp"> <TextView android:id="@+id/textViewName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_weight="8" android:text="Captain America" android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> <TextView android:textStyle="bold" android:id="@+id/textViewUpdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textViewDelete" android:layout_toLeftOf="@+id/textViewDelete" android:layout_toStartOf="@+id/textViewDelete" android:padding="5dp" android:text="Update" android:textColor="#498C1A" /> <TextView android:textStyle="bold" android:id="@+id/textViewDelete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="5dp" android:text="Delete" android:textColor="#C20A10" /> </RelativeLayout> |
Custom Adapter Class
- Create one more inner class inside MainActivity class. We will name it HeroAdapter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | class HeroAdapter extends ArrayAdapter<Hero> { //our hero list List<Hero> heroList; //constructor to get the list public HeroAdapter(List<Hero> heroList) { super(MainActivity.this, R.layout.layout_hero_list, heroList); this.heroList = heroList; } //method returning list item @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View listViewItem = inflater.inflate(R.layout.layout_hero_list, null, true); //getting the textview for displaying name TextView textViewName = listViewItem.findViewById(R.id.textViewName); //the update and delete textview TextView textViewUpdate = listViewItem.findViewById(R.id.textViewUpdate); TextView textViewDelete = listViewItem.findViewById(R.id.textViewDelete); final Hero hero = heroList.get(position); textViewName.setText(hero.getName()); //attaching click listener to update textViewUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //so when it is updating we will //make the isUpdating as true isUpdating = true; //we will set the selected hero to the UI elements editTextHeroId.setText(String.valueOf(hero.getId())); editTextName.setText(hero.getName()); editTextRealname.setText(hero.getRealname()); ratingBar.setRating(hero.getRating()); spinnerTeam.setSelection(((ArrayAdapter<String>) spinnerTeam.getAdapter()).getPosition(hero.getTeamaffiliation())); //we will also make the button text to Update buttonAddUpdate.setText("Update"); } }); //when the user selected delete textViewDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // we will display a confirmation dialog before deleting AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Delete " + hero.getName()) .setMessage("Are you sure you want to delete it?") .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //if the choice is yes we will delete the hero //method is commented because it is not yet created //deleteHero(hero.getId()); } }) .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .setIcon(android.R.drawable.ic_dialog_alert) .show(); } }); return listViewItem; } } |
Retrieving Heroes from the Database
- Create a method named readHeroes().
1 2 3 4 5 6 | private void readHeroes() { PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_READ_HEROES, null, CODE_GET_REQUEST); request.execute(); } |
- Now one more method we need to refresh the Hero List. So create a method named refreshHeroList().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | private void refreshHeroList(JSONArray heroes) throws JSONException { //clearing previous heroes heroList.clear(); //traversing through all the items in the json array //the json we got from the response for (int i = 0; i < heroes.length(); i++) { //getting each hero object JSONObject obj = heroes.getJSONObject(i); //adding the hero to the list heroList.add(new Hero( obj.getInt("id"), obj.getString("name"), obj.getString("realname"), obj.getInt("rating"), obj.getString("teamaffiliation") )); } //creating the adapter and setting it to the listview HeroAdapter adapter = new HeroAdapter(heroList); listView.setAdapter(adapter); } |
- Now also uncomment the commented method inside PeformNetworkRequest class and onCreate() method. You need to uncomment readHeroes() inside onCreate() and refreshHeroList() inside PerformNetworkRequest class.
- Now you can try running the application.

- You can see it is also working fine. Now lets do the UPDATE operation.
Update Operation
- For updating we will create a new method named updateHero().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | private void updateHero() { String id = editTextHeroId.getText().toString(); String name = editTextName.getText().toString().trim(); String realname = editTextRealname.getText().toString().trim(); int rating = (int) ratingBar.getRating(); String team = spinnerTeam.getSelectedItem().toString(); if (TextUtils.isEmpty(name)) { editTextName.setError("Please enter name"); editTextName.requestFocus(); return; } if (TextUtils.isEmpty(realname)) { editTextRealname.setError("Please enter real name"); editTextRealname.requestFocus(); return; } HashMap<String, String> params = new HashMap<>(); params.put("id", id); params.put("name", name); params.put("realname", realname); params.put("rating", String.valueOf(rating)); params.put("teamaffiliation", team); PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_UPDATE_HERO, params, CODE_POST_REQUEST); request.execute(); buttonAddUpdate.setText("Add"); editTextName.setText(""); editTextRealname.setText(""); ratingBar.setRating(0); spinnerTeam.setSelection(0); isUpdating = false; } |
- Now just uncomment the method updateHero() inside the click listener of buttonAddUpdate.
- And now we can try updating a record.

- So the update is also working absolutely fine. Now lets move to the last operation which is delete.
Delete Operation
- For delete also we need a new method. So create a method named deleteHero().
1 2 3 4 5 6 | private void deleteHero(int id) { PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_DELETE_HERO + id, null, CODE_GET_REQUEST); request.execute(); } |
- Now uncomment the method deleteHero() inside the HeroAdapter class.
- And lets test the delete operation as well.

- So it is working fine as well. And we have done with all the basic CRUD operations.
The complete code for MainActivity
- If you had some confusions following the above steps here is the complete code for MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | package net.simplifiedlearning.myheroapp; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.RatingBar; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import static android.view.View.GONE; public class MainActivity extends AppCompatActivity { private static final int CODE_GET_REQUEST = 1024; private static final int CODE_POST_REQUEST = 1025; EditText editTextHeroId, editTextName, editTextRealname; RatingBar ratingBar; Spinner spinnerTeam; ProgressBar progressBar; ListView listView; Button buttonAddUpdate; List<Hero> heroList; boolean isUpdating = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextHeroId = (EditText) findViewById(R.id.editTextHeroId); editTextName = (EditText) findViewById(R.id.editTextName); editTextRealname = (EditText) findViewById(R.id.editTextRealname); ratingBar = (RatingBar) findViewById(R.id.ratingBar); spinnerTeam = (Spinner) findViewById(R.id.spinnerTeamAffiliation); buttonAddUpdate = (Button) findViewById(R.id.buttonAddUpdate); progressBar = (ProgressBar) findViewById(R.id.progressBar); listView = (ListView) findViewById(R.id.listViewHeroes); heroList = new ArrayList<>(); buttonAddUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (isUpdating) { updateHero(); } else { createHero(); } } }); readHeroes(); } private void createHero() { String name = editTextName.getText().toString().trim(); String realname = editTextRealname.getText().toString().trim(); int rating = (int) ratingBar.getRating(); String team = spinnerTeam.getSelectedItem().toString(); if (TextUtils.isEmpty(name)) { editTextName.setError("Please enter name"); editTextName.requestFocus(); return; } if (TextUtils.isEmpty(realname)) { editTextRealname.setError("Please enter real name"); editTextRealname.requestFocus(); return; } HashMap<String, String> params = new HashMap<>(); params.put("name", name); params.put("realname", realname); params.put("rating", String.valueOf(rating)); params.put("teamaffiliation", team); PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_CREATE_HERO, params, CODE_POST_REQUEST); request.execute(); } private void readHeroes() { PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_READ_HEROES, null, CODE_GET_REQUEST); request.execute(); } private void updateHero() { String id = editTextHeroId.getText().toString(); String name = editTextName.getText().toString().trim(); String realname = editTextRealname.getText().toString().trim(); int rating = (int) ratingBar.getRating(); String team = spinnerTeam.getSelectedItem().toString(); if (TextUtils.isEmpty(name)) { editTextName.setError("Please enter name"); editTextName.requestFocus(); return; } if (TextUtils.isEmpty(realname)) { editTextRealname.setError("Please enter real name"); editTextRealname.requestFocus(); return; } HashMap<String, String> params = new HashMap<>(); params.put("id", id); params.put("name", name); params.put("realname", realname); params.put("rating", String.valueOf(rating)); params.put("teamaffiliation", team); PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_UPDATE_HERO, params, CODE_POST_REQUEST); request.execute(); buttonAddUpdate.setText("Add"); editTextName.setText(""); editTextRealname.setText(""); ratingBar.setRating(0); spinnerTeam.setSelection(0); isUpdating = false; } private void deleteHero(int id) { PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_DELETE_HERO + id, null, CODE_GET_REQUEST); request.execute(); } private void refreshHeroList(JSONArray heroes) throws JSONException { heroList.clear(); for (int i = 0; i < heroes.length(); i++) { JSONObject obj = heroes.getJSONObject(i); heroList.add(new Hero( obj.getInt("id"), obj.getString("name"), obj.getString("realname"), obj.getInt("rating"), obj.getString("teamaffiliation") )); } HeroAdapter adapter = new HeroAdapter(heroList); listView.setAdapter(adapter); } private class PerformNetworkRequest extends AsyncTask<Void, Void, String> { String url; HashMap<String, String> params; int requestCode; PerformNetworkRequest(String url, HashMap<String, String> params, int requestCode) { this.url = url; this.params = params; this.requestCode = requestCode; } @Override protected void onPreExecute() { super.onPreExecute(); progressBar.setVisibility(View.VISIBLE); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); progressBar.setVisibility(GONE); try { JSONObject object = new JSONObject(s); if (!object.getBoolean("error")) { Toast.makeText(getApplicationContext(), object.getString("message"), Toast.LENGTH_SHORT).show(); refreshHeroList(object.getJSONArray("heroes")); } } catch (JSONException e) { e.printStackTrace(); } } @Override protected String doInBackground(Void... voids) { RequestHandler requestHandler = new RequestHandler(); if (requestCode == CODE_POST_REQUEST) return requestHandler.sendPostRequest(url, params); if (requestCode == CODE_GET_REQUEST) return requestHandler.sendGetRequest(url); return null; } } class HeroAdapter extends ArrayAdapter<Hero> { List<Hero> heroList; public HeroAdapter(List<Hero> heroList) { super(MainActivity.this, R.layout.layout_hero_list, heroList); this.heroList = heroList; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View listViewItem = inflater.inflate(R.layout.layout_hero_list, null, true); TextView textViewName = listViewItem.findViewById(R.id.textViewName); TextView textViewUpdate = listViewItem.findViewById(R.id.textViewUpdate); TextView textViewDelete = listViewItem.findViewById(R.id.textViewDelete); final Hero hero = heroList.get(position); textViewName.setText(hero.getName()); textViewUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isUpdating = true; editTextHeroId.setText(String.valueOf(hero.getId())); editTextName.setText(hero.getName()); editTextRealname.setText(hero.getRealname()); ratingBar.setRating(hero.getRating()); spinnerTeam.setSelection(((ArrayAdapter<String>) spinnerTeam.getAdapter()).getPosition(hero.getTeamaffiliation())); buttonAddUpdate.setText("Update"); } }); textViewDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Delete " + hero.getName()) .setMessage("Are you sure you want to delete it?") .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { deleteHero(hero.getId()); } }) .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .setIcon(android.R.drawable.ic_dialog_alert) .show(); } }); return listViewItem; } } } |
- So after following every step correctly your application will behave as shown below.

Android MySQL Tutorial – Source Code Download
- If you are still facing troubles you can get my source code from below. It has everything, the server side scripts, database file and android project.
Android MySQL Tutorial Source Code
So thats all for this Android MySQL Tutorial friends. CRUD Operation is needed in almost every application so it is a very important thing. If you are having any confusions or queries regarding this Android MySQL tutorial don’t hesitate in asking on comments. And if you liked the post then PLEASE SHARE IT. 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.
This is great ! Thanks author, i really like your post so much and it is up to date… Thumbs UP !
Just blowed away bro..!! just trying for almost two days…bt you made it so simple…10 minutes job…bt it took more than a days count bt failed even…thankx bro…cheers (y)
Awesome tutorial !! Thank you Belal !!
Thanks a lot.great one!!really easy to understand for beginner.
AMAZING BRO! Thank you so much, you really help me!
Hi Mr.Belal ,this is very much helpful ,thank you so much for this awesome tutorial.
hi , its so helping me!,
all code above, work so well, thank you
cheers!
Awesome Stuff a big help!!!
Thank You very much, this is working
Just want to say you rock man!
Thank you very much for this tutorial, other tutorial use some depecrated class, but this one is up-to-date!
Amazing !!! great job… Never failed to surprise me for running program without error. Keep it up . Best Tut…
Thank you so much ! Helped a lot !
GREAT TUTORIAL, VERY USEFUL…THANKS
Hi Belal,
Thanks for the tutorial.
the tutorial very easy to understands.Thanks man
woow Thanks Brother Amazing …..
can’t connect to mysql and inesrt to data
so please help me
Hello VikasYadav, what error its showing, can you share details ?
I have tried many times, but always the error in the layout / activity_main.xml file in the block below:
ERROR RETURN:
Error: (52, 30) No resource found that matches the given name (at ‘entries’ with value ‘@ array / teams’).
Can anyone help?
WIN7 / 64 / ANDROID STUDIO ver 2.3.3
thanks
You just forget to create the array that we are using for the spinner items. Please check the Designing User Interface Part carefully.
Thanks. Works!! Great post. It helped a lot.
Great turorial!
I’m using this example for creating my own project.
Only i’m using Volley.
The problem is that i get an ‘Invalid API call’ error message.
How do i pass the ‘apicall’ to the php-file? Map getParams?
Thanks
public class Api {
private static final String ROOT_URL = “http://192.168.101.1/HeroApi/v1/Api.php?apicall=”;
can you give the coding how to view in another page not the same page.
The process is same.. Where you are stuck? You can check this youtube playlist https://goo.gl/oqY7Bi
This may help you
please help me sir, i am confuse at “Class to Perform Network Request” , “Create Operation” and create the method createHero().
how to input that script??? input in a same at mainactivity.java ? if true, i always failed
hello mr belal, thank you for your tutorial, i really appreciate it, this tutorial is rally easy to understand, but i have some question, i’m learning your code and because of my laziness i only make 2 attribute on my sql table, just id and name, every operation is working fine, but when im insert data and try to update there’s some error, it says
Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn’t match number of bind variables in /home/faisal/Public/Web/android_sql_api/includes/DbOperation.php on line 24
And Heres Line 24 on my DbOperation File
21. //Create operation
22. function insertData($name) {
23. $stmt = $this->con->prepare(“INSERT INTO myTable (name) VALUES (?)”);
24. $stmt->bind_param(“ssis”, $name);
25. if($stmt->execute())
26. return true;
27. return false;
28. }
And This is My Insert Case on main php
case ‘insertData’:
//first check the parameters required for this request are available or not
availableParameters(array(‘name’));
//create a new operation object
$db = new DbOperation();
//create a record by call insert method
$result = $db->insertData(
$_POST[‘name’]
);
//if the record is created adding success to response
if($result) {
//record is created means no error
$response[‘error’] = false;
//in message we have a success message
$response[‘message’] = ‘Record added successfully’;
//reload the data
$response[‘data’] = $db->getData();
} else {
//if record is not added that means there is an error
$response[‘error’] = true;
//and we have error message
$response[‘message’] = ‘some error occurred, please try again’;
}
break;
i’m on the test step and using Postman chrome app as you recommended, thank you, i really appreciate if you want to help
sorry for english
In this line
$stmt->bind_param(“ssis”, $name);
you are using only a single string to bind so it should be
$stmt->bind_param(“s”, $name);
oh, thank you very much, in that case i’ll begin to learn for more, god bless you mr belal
how to use test this online mysql database. not getting any response from the dbconnect file
hello bilal,
I have issues with the PhP scripts ,the DbConnect.php Is returning an empty page without any message
Regards
i get this error when i try to run the app
Error:(98, 70) Gradle: error: URL_CREATE_HERO has private access in Api
Error:(103, 70) Gradle: error: URL_READ_HEROES has private access in Api
Error:(137, 70) Gradle: error: URL_UPDATE_HERO has private access in Api
Error:(151, 70) Gradle: error: URL_DELETE_HERO has private access in Api
Please help
Make these constants public by adding public keyword on each line..
Sir Belal, my add form is consist of 2 activities, how will I add it in the database if fields are divided in to two activities.
If I had a chance to show you the activity, to make it clear.
Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn’t match number of bind variables in
C:\xampp\htdocs\www\includes\dboperations.php on line
28
{“error”:true,”message”:”Some error occurred please try again”}
Can you help me?
oh, no need. thanks.
I copied everything here, to have a sample app. But it doesn’t work. :3
Hi, i have a problem when i put the url on POST MAN showing this problem:
“error”: true,
“message”: “Parameters name, realname, rating, teamaffiliation missing”
what could it means? i nwe in android, please helpppp D:
Please check the video that I embedded in this post. 🙂
I have a problem
error:
JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject
Hello sir,This is really great tutorial.
I want to insert table data in thai language and retrive it in same.
i am able to insert data in thai language but unable to get response in thai.
any solution??
AoA, hi i have Coppied every thing in a sample project, but i am getting
{“error”:true,”message”:”Invalid API Call”}
this when i run API.php
kindly tell me what is this where is API calling.
P.S em newbie to PHP and andriod.
help would be appriciated thanks
Check the video embedded in this post as well.
{“error”:true,”message”:”Parameters name, realname, rating, teamaffiliation missing”}
What is this error ?? I’m getting the above error while testing api with postman
You have to send the parameters with the request as well, check the video that I embedded with this post.
Very good tutorial!
If i’m running the server on linux (Raspberry) where do I put the api folder? I’m using LAMP. Thanks
when testing the api on postman, errors occurred pls why
Warning: mysqli::__construct(): (HY000/1049): Unknown database ‘android’ in
C:\xampp\htdocs\HeroApi\includes\DbConnect.php on line
27
Failed to connect to MySQL: Unknown database ‘android’
Warning: mysqli::prepare(): Couldn’t fetch mysqli in
C:\xampp\htdocs\HeroApi\includes\DbOperation.php on line
27
Fatal error: Uncaught Error: Call to a member function bind_param() on null in C:\xampp\htdocs\HeroApi\includes\DbOperation.php:28
Stack trace:
#0 C:\xampp\htdocs\HeroApi\v1\Api.php(59): DbOperation->createHero(‘Spiderman’, ‘Peter Parker’, ‘4’, ‘Fantastic Four’)
#1 {main}
thrown in
C:\xampp\htdocs\HeroApi\includes\DbOperation.php on line
28
The error is pretty self-explaining. Did you create the database named android in phpmyadmin which you are trying to connect?
Can I use retrofit with this?
Yes obviously I have published a tutorial for this too check it here
https://www.simplifiedcoding.net/retrofit-android-tutorial/
Warning: mysqli::__construct(): (HY000/2002): No connection could be made because the target machine actively refused it.
in
C:\xampp\htdocs\HeroApi\includes\DbConnect.php on line
27
Failed to connect to MySQL: No connection could be made because the target machine actively refused it.
Warning: mysqli::prepare(): Couldn’t fetch mysqli in
C:\xampp\htdocs\HeroApi\includes\DbOperation.php on line
27
Fatal error: Uncaught Error: Call to a member function bind_param() on null in C:\xampp\htdocs\HeroApi\includes\DbOperation.php:28
Stack trace:
#0 C:\xampp\htdocs\HeroApi\v1\Api.php(59): DbOperation->createHero(‘wolverine’, ‘Logan’, ‘5’, ‘Justice League’)
#1 {main}
thrown in
C:\xampp\htdocs\HeroApi\includes\DbOperation.php on line
28
this is my error sir, already subscribed at your youtube channel sir, please help. thankyou.
Turn on your MySQL database and Go to browser type localhost/phpmyadmin and see if it is opening or not.
hi bilal thank you for this tutorials. dear bilal i got some issues :
java.net.SocketTimeoutException: failed to connect to /192.168.0.108 (port 80) after 15000ms
please help me out thanks in advance
‘W/System.err: org.json.JSONException: End of input at character 0’ of coming in when running the app
I Have the same problem 🙁
Everything is fine, no error looking to my app. but from emulator I cant create Heros, and not showing the heroes list in the list view and also not posting data to my database
Wow I found you tutorial to be very simple an step-by-step. Good job. How ever I suffer from a developmental brain damage and am disabled where even simple thing scan be very confusing to work out in practical applications. I I were you give you some money could you help me to build a fairly simple crud reader app for my website which is also for the disabled and home bound. Thank you so very much for you efforts here.
Contact me at probelalkhan@gmail.com
hi bilal thank you for this tutorials. u inspire me to learn more android program.
can i add edittext “Textwatcher” to this program ? what should i do to implement the code to make edittext “Textwatcher”. thanks in advance
edittext “Textwatcher” to filter from the listview
How do I create an ADMIN PANEL, When ever admin does those actions, all users must get a notification may be a text message, or email or simple push up notification. in android studio
Hello…..great tutorial…. but Add and update doesnt work for me… some problem in POST metod…can you helpme?
Hey, great guide, I just have a question. I would like to store the data downloaded from database somewhere inside the application as a variable so I could use it later.
I tried to read the code and also debug to see which variable is used to get data from server (php) to the actual layouts but I couldn’t find that and I have no idea where to look at. Any advice? 🙂
Hi Belal, thank you very much for your hard work whit all tutorials. I’ve have a question about the search operation. I have updated api.php and dboperations.php by adding a search function in order to select the heroes by ID. How can I implement a search field in the android application in order to display the herodisplay them in editable text fields?
Can you please help me in order to undestand how to?
Thanks you!
Regards
Great work. Well done. I want to add searchHero() method to display a single record where id is provided.
here is my code
// DbOperation.php
function searchHero($id){
$stmt = $this->con->prepare(“SELECT * FROM heros where id=?”);
$stmt->bind_param(“fk_s”, $id);
if($stmt->execute())
return true;
return false;
}
//Api.php
case ‘searchHero’:
$db = new DbOperation();
$result = $db->searchMedicineDetailsAmharic($_POST[‘id’]);
if($result){
$response[‘error’] = false;
$response[‘message’] = ‘Medicine Searched successfully’;
$response[‘medicines’] = $db->getHero();
}else{
$response[‘error’] = true;
$response[‘message’] = ‘Some error occurred please try again’;
}
break;
But when I call the funtion, it does not retrieve the result
{“error”:true,”message”:”Some error occurred please try again”}
Please help.
Did you get the solution for this problem ?
Excellent Tutorial.It works well.
I have question How do add search option when i add record.
Super Tutorial Belal Khan, even though I was looking for a tutorial that would work without using volley, retrofit or apache.
Thank you so much.
Are there any examples with dowload and image upload without the use of libraries?
What is the advantage of using third-party libraries?
I’m new to the programming world, especially android, sorry if the question was stupid.
I wait, and thank you again.
(Used by Google Translate)
Hello thank you very much for this great tutorial
When i test with an emulator it works very well but if i connect my android it does’nt work, please help me
Please tell about CRUD operations with images and text in Mysql PHP ANDROID recycler view
Thanks this works fine for me
thanks for tutorial :), this is help me
Very useful tutorial.
It explains why everything is being done, rather than just presenting a bunch of code!
I shall let you know how my App comes along!
Hi bilal.. Above code not working in my case when I connected my real device with USB and trying to get expected output but not showing anything after clicking on Add button.. Looking forward your positive response..
Amazing Tutorial and Great Writing . Well Done Belal Thanks
Thank you very much for this tutorial. You saved my time!
Thanks a lot,
The code working fine
The only problem is all the function in the same page
Organize the code according to your need. I tried to explain in the shortest way possible.
Thank You Very Much for this tutourial, Thumbs Up and keep it up, We strongly follow your tuts….
Just one question, how can I modify such code to save the data of these heroes for offline viewing (My Aim is to have an app that synchs MySQL and SQLite Database in both directions) like what Microsoft ToDo App does.
Can You Help me
you are awesome man , very very professional ..
Thank you Bilel i really appreciate this completed project
Thank you! It’s very clear for understanding. But this method is working for API 27. For API 28 I have some error with connection with database. Could you help me? I can give you more info about my problem.
How to pass multiple parameters in the url? I want to send parameters not just id. How to do that? Thanks
Thank you for your work. Does this code work on a remote database (not on the local network) ???
Yes obviously it will work. I also have an updated complete course
Check this course to learn everything about rest api, mysql and android
https://www.youtube.com/playlist?list=PLk7v1Z2rk4hhGfJw-IQCm6kjywmuJX4Rh
In this course you will learn deploying your api to a live host as well.
Hi..thank you for this tut. It has really helped me.
Though mine is a quick one. I have tested the endpoints with postman and they are all well.
Create is working perfectly on android.
The Problem is am not able to perform a GET request from server to the listview. on postman get request is working perfectly . WHille on my device it’s not.
I have gone the extra mile of downloading the source code but it’s not populating the listview. Could the problem be with my phone or what?
Hi thank you for the tutorial. It really helped me.
Though am having this issue of postman is working perfectly with CRUD service, and am able to create(POST) from android studio.
The funny thing is the data list is not populating on my device even with the source code.
What might be wrong?
Question, Can I use this approach if my database is uploaded in a web server? If yes then how? Should I just change the base url to the url of the web server where my database and .php files are located? Thanks.
Yes ofcourse, that is what we do indeed.
what if i’d like to select a single record or a user profile how can I go about it?
Awesome!
Very precise and step by step guide. Even those with a limited knowledge of android application can create this app.
thank you very much Belal for this very accurate tutorial.
One thing I will add, in case the app is not working or it is loading for a long time, is make an inbound rule on the Windows firewall Advanced Settings for httpd.exe (in my case the location of the executable is , which is the actual Apache server in case you are accessing from local area network. Also on the Advanced tab check Private under Profiles to be able to access the web server over the LAN from this Android example.
Again, thank you.
Great TuTS
I want to add a textchangedlistener to a textview so i can search through the listview if the list becomes long.
can you help?
when am clicking to add button thier is no event occured so how can i solve this error to perfrom the Add button
Try downloading my source code.
Thanks.
Good day sir, i have this error while creating a hero
error: cannot find symbol
RequestHandler requestHandler = new RequestHandler();
i badly need your help sir, Thanks!
I need help!
My API does not work! It used to work very good, but one day all of my API”s gave me errormessage.
Is there somebody who can help me?
This is the error that i get, and again… it used to work…
{“error”:true,”message”:”Invalid API Call”}
Could it have something with the PHP-version?
Belal you are really doing great work, I like your passion to make things very clear.
Thank You Friend
So what is the difference between this and a REST API?
I was struggling to find a good article about CRUD with Android, PHP, MySQL. This article is so far the best article ever, not only about the clean and good understanding information, but you can see all kindness that the author had to show to us in detail all the process with good comments inside the code, workflows, etc. But most important, all working 100 % fine! We know do documentation to explain software development is not easy not at all, you need to test and test many times before publishing your thoughts. I saw many articles where you can clearly see that the author just put over the code and when you going to run you see a lot of crashes and bugs, sometimes it’s good to learn how to fix those bugs but sometimes you need good and useful information to learn fast as possible and do your improvement by yourself after.
There are few who have the gift and the patience to teach their knowledge in the best way. Congratulations Belal Khan, I will continue to learn a lot about development with you. I hope you continue your wonderful work helping thousands of people like me. Long life. May the force be with you!
this example is helpful for me but i want unable to do with recyclerview not listview
Good Evening, I tried practicing with your code, and everything works, until creating data via the android app, after that although the data is being show in MYSQL, it does not update itself in the app list itself for update and delete operation, the layout_hero_list to be exact, I believed that there might be some mistake in my end, hence I tried downloading your original source code and ran via Android Studio and same issue. Please Help, Thank you for the tutorial.
As,salamualikum Belal Bhai,
can is use mySQL for uploading data through app like buy and sell products and also to save login and registration info in the database.
Thanks in advance…
This is not working in PHP8.1 while it works ok in 5.5
It was so far has helped with my requirement, but now the PHP version got updated by the service provider. It is not functioning.
Is there another Code you have published for PHP 8.1?
Hi Belal,Can someone help me how to get the sourcecode, because my request hasnt been approve yet, thank you. i have already subscribed to your channel
I have this error.
android:exported needs to be explicitly specified for element . Apps targeting Android 12 and higher are required to specify an explicit value for
android:exported
when the corresponding component has an intent filter definedGreat work. Going thru all your steps to test it. Could you send me the source code?