Skip to content

Instantly share code, notes, and snippets.

@AkaashSaini
Created July 7, 2021 12:06
Show Gist options
  • Save AkaashSaini/49937b2b929d51233236ce744c70ea00 to your computer and use it in GitHub Desktop.
Save AkaashSaini/49937b2b929d51233236ce744c70ea00 to your computer and use it in GitHub Desktop.
Connectivity with MySql Database
Use following steps to connect the android application with mysql server database.
• Set the permissions for HTTP connection
StrictMode.enableDefaults();
• Set uses-permission for INTERNET in AndroidManifest
• Create the object of HttpClient
HttpClient httpclient=new DefaultHttpClient();
• Create the object HttpPost or HttpGet. It represents the url to be executed.
PHP
HttpPost httppost=new HttpPost("http://10.0.2.2/android/Test.php?name="+name+"&branch="+branch);
• Create the object of HttpResponse. It executes the URL.
HttpResponse response=httpclient.execute(httppost);
• Create the object HttpEntity. It represents server response.
HttpEntity entity=response.getEntity();
• Create the object of InputStream to receive data from entity.
InputStream in=entity.getContent();
• Read response using following code
BufferedReader br = new BufferedReader(new InputStreamReader(in,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line=null;
while ((line = br.readLine()) != null)
{
sb.append(line+"\n");
}
br.close();
result=sb.toString();
• Display response result
Inserting Data in MySql Database
Steps
Suppose we want to insert name and branch in table (name, branch)
• Set the permissions for HTTP connection
StrictMode.enableDefaults();
• Set uses-permission for INTERNET in AndroidManifest.
• Create the object of HttpClient
HttpClient httpclient=new DefaultHttpClient();
• Create the object HttpPost or HttpGet
HttpPost httppost=new HttpPost("http://10.0.2.2/android/Test.php?name="+name+"&branch="+branch);
• Create the object of HttpResponse
HttpResponse response=httpclient.execute(httppost);
• Create the object HttpEntity
HttpEntity entity=response.getEntity();
• Create the object of InputStream
InputStream in=entity.getContent();
• Read response using following code
BufferedReader br = new BufferedReader(new InputStreamReader(in,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line=null;
while ((line = br.readLine()) != null)
{
sb.append(line+"\n");
}
br.close();
result=sb.toString();
• Display response result
Complete Program
public class MainActivity extends ActionBarActivity
{
EditText et1,et2;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
et1=(EditText) findViewById(R.id.editText1);
et2=(EditText) findViewById(R.id.editText2);
b1=(Button) findViewById(R.id.Button);
b1.setOnClickListener(new A());
}
class A implements View.OnClickListener
{
public void onClick(View v1)
{
String result="";
try
{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://10.0.2.2/android/Test.php?name="+name+"&branch="+branch);
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
InputStream in=entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line=null;
while ((line = br.readLine()) != null)
{
sb.append(line+"\n");
}
br.close();
result=sb.toString();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this, "Error : "+e, Toast.LENGTH_LONG).show();
}
}
}
}
Fetching Data From MySql using PHP
Write following php code
<?php
$cn=mysql_connect("localhost","root","");
if(!$cn)
{
echo"unable to connect";
die();
}
$db=mysql_select_db("android",$cn);
if(!$db)
{
echo"Database does not exist";
die();
}
$sql="select * from student";
$result=mysql_query($sql,$cn);
$n=mysql_num_rows($result);
if($n>0)
{
$rows=array();
while($rw=mysql_fetch_assoc($result))
{
$rows[]=$rw;
}
print json_encode($rows);
}
?>
Write following android code
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
b1=(Button) findViewById(R.id.button1);
tv1=(TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
//Create the object of async class
try
{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://10.0.2.2/android/ShowData.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
InputStream in=entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line=null;
while ((line = br.readLine()) != null)
{
sb.append(line+"\n");
}
br.close();
result=sb.toString();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this, "Reading : "+e, Toast.LENGTH_LONG).show();
}
try
{
JSONArray jarray=new JSONArray(result);
for(int i=0; i<jarray.length(); i++)
{
JSONObject obj=jarray.getJSONObject(i);
String text=obj.getString("name")+"\t"+obj.getString("branch");
data=data+text+"\n";
}
v.setText(data);
}
catch(Exception e)
{
Toast.makeText(MainActivity.this, "JSON : "+e, Toast.LENGTH_LONG).show();
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment