Introduction
This article explains how to send employee information to the local server in Android.
This application will show you how to send information to the server in Android. First you need a local server; I used WampServer. You can use Wamp or Xamp, whatever you like to use. I have posted an article on how to set up a WampServer on your Desktop.
Here I will show you how to create a database in WampServer.
Step 1
Open Wampserver on your browser by writing localhost or localhost:8080. In your browser, a page will be displayed.
Step 2
Click on "PhPmyadmin" on the left side.
Step 3
Click the Databases on the top of the page.
Step 4
Click on the SQL on the right side of Databases.
Step 5
Here you will create the database by writing a SQL query and click on the "Go" button. Your database will be created and you can see your database name on the left side of the page.
Step 6
Now click on your database name; another page will be shown. Now again click SQL and write a query to create a table in your database.
Now for the coding part.
Step 7
Create PHP files to connect with the server.
db_connect.php
- <?php
-
-
-
- class DB_CONNECT
- {
-
- function __construct()
- {
-
- $this->connect();
- }
-
- function __destruct()
- {
-
- $this->close();
- }
-
-
-
- function connect()
- {
-
- require_once __DIR__ . '/db_config.php';
-
- $con = mysql_connect(DB_SERVER, DB_USER, DB_WORD) or die(mysql_error());
-
- $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
-
- return $con;
- }
-
-
-
- function close()
- {
-
- mysql_close();
- }
- }
- ?>
db_config.php
- <?php
-
-
-
-
-
- define('DB_USER', "root");
- define('DB_WORD', "");
- define('DB_DATABASE', "employee");
- define('DB_SERVER', "localhost");
- ?>
Create a PHP file to store information on the server.
- <?php
-
-
-
-
-
-
-
- $response = array();
-
-
- if (isset($_POST['id']) && isset($_POST['name'])) {
-
- $id = $_POST['id'];
- $name = $_POST['name'];
-
-
- require_once __DIR__ . '/db_connect.php';
-
-
- $db = new DB_CONNECT();
-
-
- $result = mysql_query("INSERT INTO employe_data(id, name) VALUES('$id', '$name')");
-
-
- if ($result) {
-
- $response["success"] = 1;
- $response["message"] = "Product successfully created.";
-
-
- echo json_encode($response);
- } else {
-
- $response["success"] = 0;
- $response["message"] = "Oops! An error occurred.";
-
-
- echo json_encode($response);
- }
- } else {
-
- $response["success"] = 0;
- $response["message"] = "Required field(s) is missing";
-
-
- echo json_encode($response);
- }
- ?>
Step 8
Create an XML file and write this. In this XML file, you will use two text views, two edittexts, and a button.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
-
-
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Employee_Id"
- android:paddingLeft="10dip"
- android:paddingRight="10dip"
- android:paddingTop="10dip"
- android:textSize="17dip"/>
-
-
- <EditText android:id="@+id/inputId"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="5dip"
- android:layout_marginBottom="15dip"
- android:singleLine="true"/>
-
-
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Employee_Name"
- android:paddingLeft="10dip"
- android:paddingRight="10dip"
- android:paddingTop="10dip"
- android:textSize="17dip"/>
-
-
- <EditText android:id="@+id/inputName"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="5dip"
- android:layout_marginBottom="15dip"
- android:singleLine="true"
- />
-
-
-
-
-
- <Button android:id="@+id/btnCreateProduct"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Create "/>
-
- </LinearLayout>
Step 9
Create a Java class file MainActivity with the following:
Step 10
Create another Java class file JsonParser with the following:
Step 11
Android Manifest.Xml file
In the Android Manifest.xml file give the internet permission as in the following:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.adddatatotheserver"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.adddatatotheserver.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>
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- </manifest>
Step 12
Enter data to send
Progress
Data on the Server