Android Application Development With PHP and MySql DataBase
Hi Reader,
Welcome Back in my new Post with very exiting an useful topic in android application development, when we going to create an Web Base Android Application, or such application that data base required to share with several user then we have to create our application on live server.
Today i am going to explain android client server architecture.
In This Scenario our application store in PHONE ie is client and our database store on server, and client have to send a reqest to server and on behalf of that request server return response. if response is null then we not connetct to server or some internet issue.
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.apsmind.com/foodcorner/fetchfeedback.php");
HttpResponse response = httpclient.execute(httppost);
if(response!=null)
System.out.println("Connection created");
HttpEntity entity = response.getEntity();
InputStream isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
By writing these code you got your response in your InputStream Object. Now you have to convert in to text.
//convert response to string
try{
InputStreamReader isre= new InputStreamReader(isr,"iso-8859-1");
BufferedReader reader = new BufferedReader(isre,8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
String result=sb.toString();
// tv.setText(result);
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
By writing these code you got your response in your String object. But if your data in json format like this
[{"comment":"good","name":"ravikant","Rating":"3"},{"comment":"Nice app","name":"Akshu","Rating":"5"},{"comment":"nice","name":"Ravi","Rating":"5"},{"comment":"interesting","name":"amit","Rating":"5"},{"comment":"Awesum App","name":"Shobha","Rating":"5"}]
then you have to parse it.
try{
String record1 = " ";
JSONArray jArray = new JSONArray(result);
for(int i=0;i
{
JSONObject json = jArray.getJSONObject(i);
record1 = record1+"\n" +" Name : "+json.getString("name")+
"Rating: "+json.getInt("Rating")+"\n" +" Comment : "+json.getString("comment");
tv.setText(record1);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
How To Create Complete Project:-
1. Install XAMPP Server if not install yet.
2.start apache and mysql.
3. click on Mysql Admin Button :
- create an database name APSMINDTECHNOLOGY
2. create an table name courses with these field
C_ID | C_Name | C_Dur | C_Fee |
---|
C_ID | C_Name | C_Dur | C_Fee | |||
---|---|---|---|---|---|---|
1 | Java | 3 Month | 5000 | |||
2 | Android | 3 Month | 7500 | |||
3 | php++ | 4 | 8500 | |||
5 | Phonegap | 3 Month | 12000 | |||
6 | Webdesigning | 3 Month | 8500 | |||
7 | ccna | 3 Month | 9000 | |||
8 | CLOUD | 3 Month | 10000 | |||
9 | CLOUD | 3 Month | 10000 | |||
10 | c | 1 | 2000 | |||
11 | Java | 2.5 month | 5000 | |||
15 | Oractle | 3month | 8000 | |||
13 | Oractle | 3month | 8000 | |||
14 | Oractle | 3month | 8000 |
4.create an php file and save within c://XAMPP/htdocs/project/fetch.php
$server= mysql_connect("localhost","root","") or die("could not connect");
$db = mysql_select_db("APSMINDTECHNOLOGY",$server);
$result = mysql_query("select * from courses");
while($row= mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close($db);
5. Check your PhP code by this way :
open web browser and open this url http://localhost/project/fetch.php
you got result like this:
[{"C_ID":"1","C_Name":"Java","C_Dur":"3 Month","C_Fee":"5000"},{"C_ID":"2","C_Name":"Android","C_Dur":"3 Month","C_Fee":"7500"},{"C_ID":"3","C_Name":"php++","C_Dur":"4","C_Fee":"8500"},{"C_ID":"5","C_Name":"Phonegap","C_Dur":"3 Month","C_Fee":"12000"},{"C_ID":"6","C_Name":"Webdesigning","C_Dur":"3 Month","C_Fee":"8500"},{"C_ID":"7","C_Name":"ccna","C_Dur":"3 Month","C_Fee":"9000"},{"C_ID":"8","C_Name":"CLOUD","C_Dur":"3 Month","C_Fee":"10000"},{"C_ID":"9","C_Name":"CLOUD","C_Dur":"3 Month","C_Fee":"10000"},{"C_ID":"10","C_Name":"c","C_Dur":"1","C_Fee":"2000"},{"C_ID":"11","C_Name":"Java","C_Dur":"2.5 month","C_Fee":"5000"},{"C_ID":"15","C_Name":"Oractle","C_Dur":"3month","C_Fee":"8000"},{"C_ID":"13","C_Name":"Oractle","C_Dur":"3month","C_Fee":"8000"},{"C_ID":"14","C_Name":"Oractle","C_Dur":"3month","C_Fee":"8000"}]
6. Now you have to create an Android Application name ExternalDataBaseTest
i. open MainActivity.java file
package com.example.externaldatabasetest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
String record1 = " ";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView2);
new Asy().execute();
}
class Asy extends AsyncTask
{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
getData();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
tv.setText(record1);
}
}
public void getData()
{
String result = " ";
InputStream isr = null;
// To getting Data
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/androidtrainer.netne.net/courselist.php");
HttpResponse response = httpclient.execute(httppost);
if(response!=null)
System.out.println("Connection created");
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e)
{
Log.i("Error",e.toString());
// tv.setText("Connection not created");
}
//convert response to string
try{
InputStreamReader isre= new InputStreamReader(isr,"iso-8859-1");
BufferedReader reader = new BufferedReader(isre,8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
System.out.println(sb);
// tv.setText(result);
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i
{
JSONObject json = jArray.getJSONObject(i);
record1 = record1+"\n" +i+" Name : "+json.getString("C_Name")+
" Fee: "+json.getString("C_Fee");
// tv.setText(record1);
}
System.out.println(record1);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
ii. one activity_main.xml file
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
iii. add internet permission in AndroidManifest.xml
uses-permission android:name="android.permission.INTERNET"
iv.
Now you test your application in emulator or bluestack.
0 komentar: