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.

Leave a Reply

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