How to connect Android with PHP,MYSQL

android php mysql


In this post am going to explain how to connect android with php,mysql.Nowadays who are having php websites they want to develop android application through that database mysql. For php mysql android connectivity we need to design a database, php files for json output, java files for json parsing to show output.

                                                              Download Link

Youtube link for ANDROID PHP MYSQL CONNECTION



android php mysql screen1android php mysql screen2

There are three steps to follow you to connect android with php+mysql
1.MysQl Database Design
2.PHP file for JSON Output
3.Android Java files

MysQL Database Design:


CREATE TABLE IF NOT EXISTS `registration` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(100) NOT NULL,
  `sex` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

 PHP Files for JSON Output:

In this there are three files 1)Database connection 2) index.php 3) User details 

INDEX.PHP 


<?php
require_once 'db.php';


    $mysql_query=mysql_query("SELECT * FROM registration");

 $response["users"]=array();

    if(mysql_num_rows($mysql_query)>0)
{


 while($row=mysql_fetch_array($mysql_query))
 {
  $users=array();
  $users["id"]=$row['id'];
  $users["email"]=$row['email'];
  $users["sex"]=$row['sex'];

     array_push($response["users"],$users );
 
 }
 $response["success"] = 1;

 echo json_encode($response);

}
else
{
 $response["failure"]=0;
 $response["message"]="No Records found";

 echo json_encode($response);
}
?>

User_Details.php

 

0)
{


 while($row=mysql_fetch_array($mysql_query))
 {
  $users=array();
  $users["email"]=$row['email'];
  $users["sex"]=$row['sex'];

     array_push($response["users"],$users );
 
 }
 $response["success"] = 1;

 echo json_encode($response);

}
else
{
 $response["failure"]=0;
 $response["message"]="No Records found";

 echo json_encode($response);
}
}
?>

Android Files

MainActivity.java 
package info.siddhu.androidmysqlphpsqlite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView; 
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;

public class MainActivity extends Activity {
 
 ListView lv;
 
 EditText et;
 
 JSONParser jParser = new JSONParser();
 
 ArrayAdapter aa;
 
 ListAdapter adapter;
  
 // url to get all new songs list
 private static String url_all_users = "http://10.0.2.2/siddhu.info";
 
 // JSON Node names
  private static final String TAG_SUCCESS = "success";
  private static final String TAG_USERS = "users";
  private static final String TAG_UID = "id";
  private static final String TAG_EMAIL = "email";
  
  // products JSONArray
  JSONArray movies = null;

 
 // Progress Dialog
 private ProgressDialog pDialog;
 
 ArrayList> productsList;

 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  // Hashmap for ListView
     productsList = new ArrayList>();
     
     new myAsyncTask().execute();
    
  lv=(ListView)findViewById(R.id.listView1);
  et=(EditText)findViewById(R.id.editText1);
  
  lv.setOnItemClickListener(new OnItemClickListener() {

   @Override
public void onItemClick(AdapterView parent, View view,int position, long id) 
   {
    
    // getting values from selected ListItem
String uid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
    
    // Starting new intent
 Intent i = new Intent(getApplicationContext(),user_details.class);
    
\Toast.makeText(getApplicationContext(), uid, Toast.LENGTH_SHORT).show();
    // sending pid to next activity
   i.putExtra(TAG_UID, uid);
    
    startActivity(i);
    
   
   }
  });
  
  
  
  
  
 }



class myAsyncTask extends AsyncTask {

//Executed on the UI thread before the
//time taking task begins
protected void onPreExecute() 
{
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Users. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

//Executed on a special thread and all your
//time taking tasks should be inside this method
protected String doInBackground(String... args) 
{
 List params=new ArrayList();
 JSONObject json=jParser.makeHttpRequest(url_all_users, "GET", params);
 try
 {
  int success=json.getInt(TAG_SUCCESS);
  if(success==1)
  {
   movies=json.getJSONArray(TAG_USERS);
   for (int i = 0; i < movies.length(); i++) 
   {
    JSONObject c = movies.getJSONObject(i);

    // Storing each json item in variable
    String id = c.getString(TAG_UID);
    String name = c.getString(TAG_EMAIL);

    // creating new HashMap
HashMap map = new HashMap();

    
    map.put(TAG_UID, id);
    map.put(TAG_EMAIL, name);

    // adding HashList to ArrayList
    productsList.add(map);
   }
  }
  
 }
 catch (JSONException e) {
  // TODO: handle exception
  e.printStackTrace();
 }

return null;
}
//Executed on the UI thread after the
//time taking process is completed
protected void onPostExecute(String file_url)
{
 // dismiss the dialog after getting all products
 pDialog.dismiss();
 // updating UI from Background Thread
 runOnUiThread(new Runnable() {
  public void run() {
   /**
    * Updating parsed JSON data into ListView
    * */
adapter = new SimpleAdapter(MainActivity.this, productsList,R.layout.list_item,
 new String[] 
{ TAG_UID,TAG_EMAIL},new int[] { R.id.pid, R.id.name });
   // updating listview
   lv.setAdapter(adapter);
  }
 });

}
}
}

Extra files placed in download link please download it and fell free to comment here if you have any doubt.

Why Big Data?

why big data
In my previous post i wrote intro on Big Data ( What is Big Data? ). Now i want to explain why Big Data , why it become soo much popular in little span. What are the parameters that raise the market of Big Data.

Do you know? Huge competetion in the market:

Retails-Customer Analytics
Travel-Travel Pattern of customer
Website-Understand user navigation pattern,interest,conversion,etc
Sensors,Satellite,geospatial data
Millitary and Intelligence

Big Data in Websites

I can clearly explain with clean example, If we take Amazon,Flipkart there is large amount of  data but they are providing results very fast, transactions doing very securly how? Because of Big Data. When you  opened any product you can find recommendations related to that products , by analyzing customer clicks,products they programmed.

How Big Data Used in Millitary , Intelligence

In recent research they declared that large amount of data stored in millitary databases. How? Means they will store each and every information of  ours. For that they need Big Data Concept.

Download Below Books to get Knowledge More



What's Next

Essentials of Big Data
What are the Conventional Approaches Before Big Data?
Problems with conventional Approaches


What is Big Data?

what is bigdata

In this post i want to explain what is Big Data, introduction to big data, different dimensions about the big data, why Big Data became popular,familiar in short span? Before the big data there are some conventional technologies , what were those, why the conventional technologies were failed , i will explain in this post.

What is Big Data?

1.Every data world create 2.5 quintillion bytes of data so much that 90% of the data in the world today has been created in the last two years alone.
2.Gartner defines that  Big Data as high volume,velocity and variety information assets that demand cost effective,innovative forms of information processing for enhanced insight and decission making.
3.According to the IBM, 80% of data captured today is unstructured, from sensors used to gather climate information,posts to social media sites, digital pictures and videos,purchase transaction records, and cell phone gps signals, to name a few. all of this unstructured data is Big Data.

Unstructred data nothing but data not in standard format,simply video images, social networking data etc.,
Now a days every data is in rows and coloumns i mean in table, but to learn Big Data we should think to store data in Buckets.

Best Books i perefer to learn Hadoop,Big Data Download it



What's Next

Follow the next articles to get clear idea about Big Data.
 
What is Volume,Variety,Velocity in Big Data?
What are the Conventional Approaches Before Big Data?
Problems with conventional Approaches
 

Best Features of Struts-2

struts-2 features

Struts-2 Features Tutorial:

struts-2 provides many features that was not provide on strust1,Important features of struts-2 followed below

1)Configurable MVC Componenets
2)POJO Based Actions
3)AJAX Support
4)Integration Support
5)Various Result Types
6)Various Tag Support
7)Theme and Template Support

1)Configuable MVC Components

in struts-2 framework we provide all components(Model View Controller) information in struts.xml file.If we need to change any information,we simply change it in the xml.

2)POJO Based Actions

in struts-2 ,action class in POJO(Plain old java object) a simple java class here you are not forced to implement any interface or inherit any class.

3)AJAX Support

Ajax provides performance fast, it will send parameter to the other files without refreshing,it is used to make asynchronous request.

4)Integration Support

we can integrate struts-2 with other frameworks like hibernate,play,etc..

5)Various Result Types 

we can use JSP,Sevlets results to integrate here.

6) Various Tag Support

struts-2 provides varioys types of tags UI Tags, Data Tags, Control Tags to make development easy.

Struts-2 Basic tutorial

structs-2 tutorial

Tutorial:

The struts-2 framework is used to develop Model View Controller Based Web Application.

The struts-2 was developed by Criag Mcclanahan and donated to ApacheFoundation in May 2010 and struts 1.0 was released in june 2001.

This struts-2 tutorial covers all topics of framework for beginners and experienced persons.

Struts-2 Framework:

Struts -2 is a combination of webbwork and struts1.

Struts-2=Webwork+struts1

The struts-2 provides the pojo based java classes,validation support,AJAX Integration,various framework support.


How to Design Website for Mobiles

viewport meta tag for mobile website design
 How to design website for mobile devices and for smartphones,The next world with smart phones so start website with moble templates.In this post i want to explain very basic tip to explain the meta viewport tag

Normal Site:
Contains HTML code with CSS
<!DOCTYPE html>
<title>Website</title>
<style>
#container
{
width:800px;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
</html>
Mobile Site:  Contains HTML code. CSS width style with percentage. width : 100% . Take a look at META tag viewport.

<html>
<head>
<title>Mobile Website</title>
<meta content="width=device-width; initial-scale=1.0; 
maximum-scale=1.0; user-scalable=0;" name="viewport" />
<style>
#container
{
width:100%;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
</html>
Meta Tag for Mobile Phones: Auto Adjustment Mobile
<meta content = "width=device-width; initial-scale=1.0; 
maximum-scale=1.0; user-scalable=0;" name = "viewport" />