Introduction

Android is one of the most popular operating systems for mobile. A remote database means that you can access the data from this database in a remote location. A lot of applications are used to send the data to the remote database module. I will show you how to send the data to the remote database in an Android Application, using Android Studio.
Requirements
To install and configure XAMPP Server, the reference article is given below.
Steps should be followed
Carefully follow my steps to send the data to the remote database in an Android Application, using Android Studio and I have included the source code too.
Step 1
Open Android Studio to start the new project.
android
Step 2
Put the Application name and the company domain. If you wish to use C++ to code the project, include C++ support, followed by clicking Next.
android
Step 3
Select Android Minimum SDK. After you chose the minimum SDK, it will show the approximate percentage of people using that SDK. Just click "Next".
android
Step 4
Choose the Empty activity, followed by clicking Next.
android
Step 5
Put the activity name and the layout name. Android Studio basically takes Java class names. Provide the activity name and click Finish.
android
Step 6
Start Apache and MySQL Server in XAMPP Server.
android
Step 7
Create the database data_user and then click Create. Afterward, create the table user_info.
android
data_user MySQL
  1. CREATE TABLE `user_info` (
  2. `name` varchar(50) NOT NULL,
  3. `email` varchar(50) NOT NULL
  4. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  5. --
  6. -- Dumping data for table `user_info`
  7. --
  8. INSERT INTO `user_info` (`name`, `email`) VALUES
  9. ('', ''),
  10. ('', ''),
  11. ('', ''),
  12. ('', ''),
  13. ('', ''),
  14. ('', ''),
  15. ('', ''),
  16. ('', ''),
  17. ('', ''),
  18. ('', ''),
  19. ('', ''),
  20. ('', ''),
  21. ('', ''),
  22. ('', '');
  23. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  24. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  25. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Step 9
Connect to the database using the following PHP script.
Dbconfig.php
  1. <?php
  2. $user_name = isset($_POST["name"]);
  3. $user_pass = isset($_POST["email"]);
  4. $user = "root";
  5. $password = "";
  6. $host ="localhost";
  7. $db_name ="data_user";
  8. $con = mysqli_connect($host,$user,$password,$db_name);
  9. $sql = "insert into user_info values('".$user_name."','".$user_pass."');";
  10. if(mysqli_query($con,$sql))
  11. {
  12. echo "Data inserted successfully....";
  13. }
  14. else
  15. {
  16. echo "some error occured";
  17. }
  18. mysqli_close($con);
  19. ?>
android
android
Step 10
Open your browser and run the dbconfig.php page.
android
Step 11
Go to activity_main.xml, followed by clicking Text bottom. This XML file contains designing the code for an Android app. In activity_main.xml, copy and paste the code given below.
Activity_main.xml code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="ganeshannt.remotedata.MainActivity">
  8. <EditText
  9. android:id="@+id/name1"
  10. android:layout_width="378dp"
  11. android:layout_height="53dp"
  12. android:layout_marginRight="4dp"
  13. android:ems="10"
  14. android:inputType="textPersonName"
  15. android:text=""
  16. app:layout_constraintRight_toRightOf="parent"
  17. tools:layout_editor_absoluteY="119dp"
  18. tools:layout_editor_absoluteX="2dp"
  19. android:layout_above="@+id/email"
  20. android:layout_alignEnd="@+id/name" />
  21. <EditText
  22. android:id="@+id/email1"
  23. android:layout_width="379dp"
  24. android:layout_height="54dp"
  25. android:ems="10"
  26. android:inputType="textPersonName"
  27. android:text=""
  28. tools:layout_editor_absoluteX="3dp"
  29. tools:layout_editor_absoluteY="245dp"
  30. android:layout_above="@+id/bn"
  31. android:layout_alignParentEnd="true"
  32. android:layout_marginBottom="24dp"
  33. android:layout_below="@+id/email"
  34. android:layout_alignParentStart="true" />
  35. <TextView
  36. android:id="@+id/name"
  37. android:layout_width="380dp"
  38. android:layout_height="46dp"
  39. android:textSize="20dp"
  40. android:text="Enter Name"
  41. tools:layout_editor_absoluteX="-2dp"
  42. tools:layout_editor_absoluteY="61dp"
  43. android:layout_above="@+id/name1"
  44. android:layout_alignParentStart="true" />
  45. <TextView
  46. android:id="@+id/email"
  47. android:layout_width="666dp"
  48. android:layout_height="42dp"
  49. android:text="Enter the Email"
  50. android:textSize="20dp"
  51. tools:layout_editor_absoluteX="0dp"
  52. tools:layout_editor_absoluteY="205dp"
  53. android:layout_centerVertical="true"
  54. android:layout_alignParentEnd="true" />
  55. <Button
  56. android:id="@+id/bn"
  57. android:layout_width="380dp"
  58. android:layout_height="72dp"
  59. android:text="SUBMIT"
  60. tools:layout_editor_absoluteX="2dp"
  61. tools:layout_editor_absoluteY="351dp"
  62. android:layout_alignParentBottom="true"
  63. android:layout_alignEnd="@+id/name1"
  64. android:layout_marginBottom="81dp" />
  65. </RelativeLayout>
Step 12
In MainActivity.java, copy and paste the code given below. Java programming is the back-end language for Android. Do not replace your package name otherwise, the app will not run. The code given below contains my package name.
MainActivity.java code
  1. package ganeshannt.remotedata;
  2. import android.content.DialogInterface;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10. import com.android.volley.AuthFailureError;
  11. import com.android.volley.Request;
  12. import com.android.volley.Response;
  13. import com.android.volley.VolleyError;
  14. import com.android.volley.toolbox.StringRequest;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. public class MainActivity extends AppCompatActivity {
  18. Button button;
  19. EditText Name , Email;
  20. String server_url = "http://192.168.43.246/app/dbconfig.php";
  21. AlertDialog.Builder builder;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. button = (Button) findViewById(R.id.bn);
  27. Name = (EditText) findViewById(R.id.name1);
  28. Email = (EditText) findViewById(R.id.email1);
  29. builder = new AlertDialog.Builder(MainActivity.this);
  30. button.setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. final String name , email ;
  34. name =Name.getText().toString();
  35. email=Email.getText().toString();
  36. StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {
  37. @Override
  38. public void onResponse(String response) {
  39. builder.setTitle("Server Response");
  40. builder.setMessage("Response :"+response);
  41. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  42. @Override
  43. public void onClick(DialogInterface dialog, int which) {
  44. Name.setText("");
  45. Email.setText("");
  46. }
  47. });
  48. AlertDialog alertDialog = builder.create();
  49. alertDialog.show();
  50. }
  51. }
  52. , new Response.ErrorListener() {
  53. @Override
  54. public void onErrorResponse(VolleyError error) {
  55. Toast.makeText(MainActivity.this,"some error found .....",Toast.LENGTH_SHORT).show();
  56. error.printStackTrace();
  57. }
  58. }){
  59. @Override
  60. protected Map<String, String> getParams() throws AuthFailureError {
  61. Map <String,String> Params = new HashMap<String, String>();
  62. Params.put("name",name);
  63. Params.put("email",email);
  64. return Params;
  65. }
  66. };
  67. Mysingleton.getInstance(MainActivity.this).addTorequestque(stringRequest);
  68. }
  69. });
  70. }
  71. }
Step 13
Create a new Java file to request the message queue.
android
Java class name is Mysingleton.
android
Step 14
Open your MainActivity.java file. Here, you need to provide your current IP Address to connect to the local server (XAMPP).
android
Step 15
Open your command prompt and type ipconfig. It will show you your current IP address.
android
android
Step 16
In Mysingleton.java, copy and paste the code given below. Java is the back-end language for Android. Do not replace your package name here also. The code given below contains my package name.
Mysingleton.java code
  1. package ganeshannt.remotedata;
  2. import android.content.Context;
  3. import com.android.volley.Request;
  4. import com.android.volley.RequestQueue;
  5. import com.android.volley.toolbox.Volley;
  6. /**
  7. * Created by For on 4/24/2017.
  8. */
  9. public class Mysingleton {
  10. private static Mysingleton mInstance;
  11. private RequestQueue requestQueue;
  12. private static Context mCtx;
  13. private Mysingleton (Context Context)
  14. {
  15. mCtx = Context;
  16. requestQueue = getRequestQueue();
  17. }
  18. public static synchronized Mysingleton getInstance (Context context)
  19. {
  20. if (mInstance==null)
  21. {
  22. mInstance =new Mysingleton(context);
  23. }
  24. return mInstance;
  25. }
  26. public RequestQueue getRequestQueue() {
  27. if (requestQueue==null)
  28. {
  29. requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
  30. }
  31. return requestQueue;
  32. }
  33. public<T> void addTorequestque(Request<T> request)
  34. {
  35. requestQueue.add(request);
  36. }
  37. }
Step 17
Add the dependencies given below into the build.gradle file. compile 'com.mcxiaoke.volley:library:1.0.19' compile 'com.android.volley:volley:1.0.0'
Step 18
For allowing the internet connection, you need to add the code given below into AndroidManifest.xml.
  1. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
Step 20
This is our user interface of the Application. Click the "Make project" option.
android
Step 21
Run the Application, followed by choosing the virtual machine. Now, click "OK".
android
Deliverables
Here, it has successfully sent the data to the remote database in the recently created Android application.
android
Enter your name and an Email Id, followed by clicking "Submit".
android
After the data insertion, it will show you the Server response popup message.
android
Don’t forget to like and follow me. If you have any doubts, just comment below.
Source code
https://github.com/GaneshanNT/RemoteData