Send Insert EditText data online to MySQL db server in android

How to submit multiple values online from android app to PhpMyAdmin database by calling website URL.

What we are doing in this project : We are sending EditText values(Name, Email, Website name) to server directly via HTTP protocols by calling online website URL. So from here we have just send the data by calling a particular URL and from that URL on the server side we have been receiving data using PHP script $Post method. After receiving data we are inserting that amount of data directly into MySQL database. So here is the complete step by step tutorial for Send Insert EditText data online to MySQL db server in android.

android-project-download-code-button

How to Send Insert EditText data online to MySQL db server in android.

Send Insert EditText data online to MySQL db server in android

List of Files or Folder that is included inside the download project :

  1. Pre build MySQL database .SQL file.
  2. insert_data.php file.
  3. The whole Android project.

Note : Please add internet permission to your AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET"/>

Note: If you are facing The import org.apache.http.HttpResponce cannot resolved error solution then read my this tutorial for solve this problem.
Code for MainActivity.java file.

 package com.submitandroiddata2server_android_examples.com;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

 EditText editTextName, editTextEmail, editTextWebsite;
 
 String GetName, GetEmail, GetWebsite;
 
 Button buttonSubmit ;
 
 String DataParseUrl = "http://demoblog.16mb.com/insert_data.php";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 editTextName = (EditText)findViewById(R.id.editText1);
 editTextEmail = (EditText)findViewById(R.id.editText2);
 editTextWebsite = (EditText)findViewById(R.id.editText3);
 
 buttonSubmit = (Button)findViewById(R.id.button1);
 
 buttonSubmit.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 GetDataFromEditText();
 
 SendDataToServer(GetName, GetEmail, GetWebsite);
 
 }
 });
 }
 
 
 public void GetDataFromEditText(){
 
 GetName = editTextName.getText().toString();
 GetEmail = editTextEmail.getText().toString();
 GetWebsite = editTextWebsite.getText().toString();
 
 }
 
 
 public void SendDataToServer(final String name, final String email, final String website){
 class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
 @Override
 protected String doInBackground(String... params) {
 
 String QuickName = name ;
 String QuickEmail = email ;
 String QuickWebsite = website;
 
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
 
 nameValuePairs.add(new BasicNameValuePair("name", QuickName));
 nameValuePairs.add(new BasicNameValuePair("email", QuickEmail));
 nameValuePairs.add(new BasicNameValuePair("website", QuickWebsite));
 
 try {
 HttpClient httpClient = new DefaultHttpClient();
 
 HttpPost httpPost = new HttpPost(DataParseUrl);
 
 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 
 HttpResponse response = httpClient.execute(httpPost);
 
 HttpEntity entity = response.getEntity();
 
 
 } catch (ClientProtocolException e) {
 
 } catch (IOException e) {
 
 }
 return "Data Submit Successfully";
 }
 
 @Override
 protected void onPostExecute(String result) {
 super.onPostExecute(result);
 
 Toast.makeText(MainActivity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();
 
 }
 }
 SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
 sendPostReqAsyncTask.execute(name, email, website);
 }
 
}

Code for activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.submitandroiddata2server_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:ems="10"
 android:hint="Enter Your Name" >
 </EditText>

 <EditText
 android:id="@+id/editText2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="33dp"
 android:ems="10"
 android:hint="Enter Your Email" />

 <EditText
 android:id="@+id/editText3"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="33dp"
 android:ems="10"
 android:hint="Enter Your Website" />

 <Button
 android:id="@+id/button1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Submit Data To Server" />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.submitandroiddata2server_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.INTERNET"/>
 
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Code for insert_data.php file.

 <?php

//This script is designed by Android-Examples.com
//Define your host here.
$hostname = "mysql.hostinger.in";
//Define your database username here.
$username = "u727224026_demo";
//Define your database password here.
$password = "1234567890";
//Define your database name here.
$dbname = "u727224026_demo";
 
 $con = mysqli_connect($hostname,$username,$password,$dbname);
 
 $name = $_POST['name'];
 $email = $_POST['email'];
 $website = $_POST['website'];
 
 
 $Sql_Query = "insert into GetDataTable (name,email,website) values ('$name','$email','$website')";
 
 if(mysqli_query($con,$Sql_Query)){
 
 echo 'Data Inserted Successfully';
 
 }
 else{
 
 echo 'Try Again';
 
 }
 mysqli_close($con);
?>

Screenshots:

parse-data

parse-data-2

parse-data-3

DB-SCREENSHOT

Click here to download Send Insert EditText data online to MySQL db server in android project with source code.

53 Comments

  1. location of .php file is ??

  2. Ajay, you have to upload .php file on your web server and pass the insert_data.php file URL into DataParseUrl variable which is defined in MainActivity.java file

  3. Thank you for this. It works perfectly! How do I upload a image? Do you have a tutorial on this or could you maybe make one?

  4. Hi juned..I pasted the above code..The toast message shows the data is inserted successfully but the table isn’t updated.. :/ plz help;

  5. hi juned, I am facing an issue in inserting values into database,. The data is getting inserted only if it is in Mainactivity..
    “failed to make and chown /acct/uid_10058: Read-only file system”
    This is the logcat error i always get.plz help

  6. Hey its very helpful,But i need help for login code send it if u have

  7. Hi juned..I pasted the above code..The toast message shows the data is inserted successfully but the table isn’t updated

  8. yes i did.when i run php file separately it shows connected successfully but from android, no updation of the table and i added permissions too

  9. Hay Juned I have problem, table in data base no update and still 0, can u help me? or can i sent my program so u can correct my program ?

  10. Thanks ,good tutorial
    I want to save image as blob datatype into MySQL database, how to do it , please help me ….Thanks again

  11. why i call params in code this : protected String doInBackground(String… params). cannot resolve symbol params ?… how to fix this ?…

    • williams this is for background task if you have facing any error then please download the whole project and import it into studio.

  12. its not working for meee
    there is no errrors but it pushing null to database

  13. using API 22….god man developing for android totally screwed up due to code and OS fragmentation.

  14. How I know ‘$hostname’ in php file

  15. Hi, since HttpClient and HttpPost are deprecated for API 23 so im having trouble converting it in HttpURLConnection. Can you help me Please.

  16. The Following Error
    Error:Execution failed for task ‘:app:transformResourcesWithMergeJavaResForDebug’.
    > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
    File1: F:\Adventure\18 Using Networking Library – The Volley Networking Library\MyApplication3\app\libs\fluent-hc-4.5.3.jar
    File2: F:\Adventure\18 Using Networking Library – The Volley Networking Library\MyApplication3\app\libs\httpcore-4.4.6.jar
    File3: F:\Adventure\18 Using Networking Library – The Volley Networking Library\MyApplication3\app\libs\httpmime-4.5.3.jar

  17. hello ser… can i send data even the server and device dont have the same wifi connection tnx

  18. Hi sir! I’ve tried your codes. its working but when i input data in the mobile app and submit it, name, email and website isn’t in my database but the id number only. Can you help me out? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *