Hi Friends,
This is all about linking your Android app with MySQL DB using php.
Requirements
1.DB SQL
2. PHP Script
3. Android java file
4. Manifest file with permission
1. DB SQL
--
-- Database: `snit`
--
-- --------------------------------------------------------
--
-- Table structure for table `student`
--
CREATE TABLE IF NOT EXISTS `student` (
`name` text NOT NULL,
`rollno` int(11) NOT NULL,
`marks` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2. PHP Script
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("SNIT",$con);
mysql_query("insert into student values('ARJUN',108,100)");
echo "HUPATFTTU";
?>
NB: Save in a folder 's1mca' in the server
3. Android java file - HUP3Activity.java
package hup3.h3;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class HUP3Activity extends Activity {
Button bt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast toast=Toast.makeText(getApplicationContext(), "HUP", Toast.LENGTH_LONG);
//toast.show();
BufferedReader bufferedReader = null;
try {
URL url = new URL("http://10.0.2.2/s1mca/savestud.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result = bufferedReader.readLine();
bt.setText(result);
//return result;
}catch(Exception e){
bt.setText(e.toString());}
}
});
}
}
NB: The response from the PHP page is set as the caption of the Button
4.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hup3.h3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".HUP3Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
NB: Note the line highlighted in blue. It is mandatory
This is all about linking your Android app with MySQL DB using php.
Requirements
1.DB SQL
2. PHP Script
3. Android java file
4. Manifest file with permission
1. DB SQL
--
-- Database: `snit`
--
-- --------------------------------------------------------
--
-- Table structure for table `student`
--
CREATE TABLE IF NOT EXISTS `student` (
`name` text NOT NULL,
`rollno` int(11) NOT NULL,
`marks` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2. PHP Script
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("SNIT",$con);
mysql_query("insert into student values('ARJUN',108,100)");
echo "HUPATFTTU";
?>
NB: Save in a folder 's1mca' in the server
3. Android java file - HUP3Activity.java
package hup3.h3;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class HUP3Activity extends Activity {
Button bt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast toast=Toast.makeText(getApplicationContext(), "HUP", Toast.LENGTH_LONG);
//toast.show();
BufferedReader bufferedReader = null;
try {
URL url = new URL("http://10.0.2.2/s1mca/savestud.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result = bufferedReader.readLine();
bt.setText(result);
//return result;
}catch(Exception e){
bt.setText(e.toString());}
}
});
}
}
NB: The response from the PHP page is set as the caption of the Button
4.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hup3.h3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".HUP3Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
NB: Note the line highlighted in blue. It is mandatory