Send Data To The Remote Database In Android Applications

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.   `namevarchar(50) NOT NULL,  
  3.   `email` varchar(50) NOT NULL  
  4. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;  
  5.   
  6. --  
  7. -- Dumping data for table `user_info`  
  8. --  
  9.   
  10. INSERT INTO `user_info` (`name`, `email`) VALUES  
  11. (''''),  
  12. (''''),  
  13. (''''),  
  14. (''''),  
  15. (''''),  
  16. (''''),  
  17. (''''),  
  18. (''''),  
  19. (''''),  
  20. (''''),  
  21. (''''),  
  22. (''''),  
  23. (''''),  
  24. ('''');  
  25.   
  26. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;  
  27. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;  
  28. /*!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.   
  9.   
  10.     <EditText  
  11.         android:id="@+id/name1"  
  12.         android:layout_width="378dp"  
  13.         android:layout_height="53dp"  
  14.         android:layout_marginRight="4dp"  
  15.         android:ems="10"  
  16.         android:inputType="textPersonName"  
  17.         android:text=""  
  18.         app:layout_constraintRight_toRightOf="parent"  
  19.         tools:layout_editor_absoluteY="119dp"  
  20.         tools:layout_editor_absoluteX="2dp"  
  21.         android:layout_above="@+id/email"  
  22.         android:layout_alignEnd="@+id/name" />  
  23.   
  24.     <EditText  
  25.         android:id="@+id/email1"  
  26.         android:layout_width="379dp"  
  27.         android:layout_height="54dp"  
  28.         android:ems="10"  
  29.         android:inputType="textPersonName"  
  30.         android:text=""  
  31.         tools:layout_editor_absoluteX="3dp"  
  32.         tools:layout_editor_absoluteY="245dp"  
  33.         android:layout_above="@+id/bn"  
  34.         android:layout_alignParentEnd="true"  
  35.         android:layout_marginBottom="24dp"  
  36.         android:layout_below="@+id/email"  
  37.         android:layout_alignParentStart="true" />  
  38.   
  39.     <TextView  
  40.         android:id="@+id/name"  
  41.         android:layout_width="380dp"  
  42.         android:layout_height="46dp"  
  43.         android:textSize="20dp"  
  44.         android:text="Enter Name"  
  45.         tools:layout_editor_absoluteX="-2dp"  
  46.         tools:layout_editor_absoluteY="61dp"  
  47.         android:layout_above="@+id/name1"  
  48.         android:layout_alignParentStart="true" />  
  49.   
  50.     <TextView  
  51.         android:id="@+id/email"  
  52.         android:layout_width="666dp"  
  53.         android:layout_height="42dp"  
  54.         android:text="Enter the Email"  
  55.         android:textSize="20dp"  
  56.         tools:layout_editor_absoluteX="0dp"  
  57.         tools:layout_editor_absoluteY="205dp"  
  58.         android:layout_centerVertical="true"  
  59.         android:layout_alignParentEnd="true" />  
  60.   
  61.     <Button  
  62.         android:id="@+id/bn"  
  63.         android:layout_width="380dp"  
  64.         android:layout_height="72dp"  
  65.         android:text="SUBMIT"  
  66.         tools:layout_editor_absoluteX="2dp"  
  67.         tools:layout_editor_absoluteY="351dp"  
  68.         android:layout_alignParentBottom="true"  
  69.         android:layout_alignEnd="@+id/name1"  
  70.         android:layout_marginBottom="81dp" />  
  71. </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.     
  3. import android.content.DialogInterface;    
  4. import android.os.Bundle;    
  5. import android.support.v7.app.AlertDialog;    
  6. import android.support.v7.app.AppCompatActivity;    
  7. import android.view.View;    
  8. import android.widget.Button;    
  9. import android.widget.EditText;    
  10. import android.widget.Toast;    
  11.     
  12. import com.android.volley.AuthFailureError;    
  13. import com.android.volley.Request;    
  14. import com.android.volley.Response;    
  15. import com.android.volley.VolleyError;    
  16. import com.android.volley.toolbox.StringRequest;    
  17.     
  18. import java.util.HashMap;    
  19. import java.util.Map;    
  20.     
  21. public class MainActivity extends AppCompatActivity {    
  22.     Button button;    
  23.     EditText Name , Email;    
  24.     String server_url = "http://192.168.43.246/app/dbconfig.php";    
  25.     AlertDialog.Builder builder;    
  26.     
  27.     @Override    
  28.     protected void onCreate(Bundle savedInstanceState) {    
  29.         super.onCreate(savedInstanceState);    
  30.         setContentView(R.layout.activity_main);    
  31.     
  32.         button = (Button) findViewById(R.id.bn);    
  33.         Name = (EditText) findViewById(R.id.name1);    
  34.         Email = (EditText) findViewById(R.id.email1);    
  35.         builder = new AlertDialog.Builder(MainActivity.this);    
  36.         button.setOnClickListener(new View.OnClickListener() {    
  37.             @Override    
  38.             public void onClick(View v) {    
  39.                 final String name , email ;    
  40.                 name =Name.getText().toString();    
  41.                 email=Email.getText().toString();    
  42.                 StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {    
  43.                     @Override    
  44.                     public void onResponse(String response) {    
  45.     
  46.                         builder.setTitle("Server Response");    
  47.                         builder.setMessage("Response :"+response);    
  48.                         builder.setPositiveButton("OK"new DialogInterface.OnClickListener() {    
  49.                             @Override    
  50.                             public void onClick(DialogInterface dialog, int which) {    
  51.                                 Name.setText("");    
  52.                                 Email.setText("");    
  53.                             }    
  54.                         });    
  55.                         AlertDialog alertDialog = builder.create();    
  56.                         alertDialog.show();    
  57.     
  58.                     }    
  59.                 }    
  60.     
  61.                         , new Response.ErrorListener() {    
  62.                     @Override    
  63.                     public void onErrorResponse(VolleyError error) {    
  64.                         Toast.makeText(MainActivity.this,"some error found .....",Toast.LENGTH_SHORT).show();    
  65.                         error.printStackTrace();    
  66.     
  67.                     }    
  68.                 }){    
  69.                     @Override    
  70.                     protected Map<String, String> getParams() throws AuthFailureError {    
  71.                         Map <String,String> Params = new HashMap<String, String>();    
  72.                         Params.put("name",name);    
  73.                         Params.put("email",email);    
  74.                         return Params;    
  75.     
  76.                     }    
  77.                 };    
  78.                 Mysingleton.getInstance(MainActivity.this).addTorequestque(stringRequest);    
  79.             }    
  80.         });    
  81.     
  82.     }    

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.     
  3. import android.content.Context;    
  4.     
  5. import com.android.volley.Request;    
  6. import com.android.volley.RequestQueue;    
  7. import com.android.volley.toolbox.Volley;    
  8.     
  9. /**  
  10.  * Created by For on 4/24/2017.  
  11.  */    
  12. public class Mysingleton {    
  13.     private static Mysingleton mInstance;    
  14.     private RequestQueue requestQueue;    
  15.     private static Context mCtx;    
  16.     private Mysingleton (Context Context)    
  17.     {    
  18.         mCtx = Context;    
  19.         requestQueue = getRequestQueue();    
  20.     }    
  21.     
  22.     public static  synchronized Mysingleton getInstance (Context context)    
  23.     {    
  24.         if (mInstance==null)    
  25.         {    
  26.             mInstance =new Mysingleton(context);    
  27.         }    
  28.         return mInstance;    
  29.     }    
  30.     
  31.     public RequestQueue getRequestQueue() {    
  32.     
  33.         if (requestQueue==null)    
  34.         {    
  35.             requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());    
  36.         }    
  37.         return requestQueue;    
  38.     
  39.     }    
  40.     
  41.     public<T> void addTorequestque(Request<T> request)    
  42.     {    
  43.         requestQueue.add(request);    
  44.     }    
  45. }   
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