Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/01/2016, 09:42
maximusinternet
 
Fecha de Ingreso: diciembre-2015
Mensajes: 3
Antigüedad: 8 años, 4 meses
Puntos: 0
Datos dinámicos base de datos

Tengo una base de datos en mysql que administro con php admin, los datos son consultados por una aplicacion en android a través de PHP y JSON, todo hasta aquí esta perfecto, lo que quiero es que cada vez que realice una modificación en phpadmin de la base de datos como actualizar, insertar eliminar registros, esto se vea reflejado en tiempo real o de forma instantánea en la app sin necesidad de tener que cerrar y abrir la app para que esto ocurra.. cual quier sugerencia, comentario se lo agradecere mucho, saludos y gracias por su atencion.

Aquí la clase:

Código:
public class VerA extends ActionBarActivity {

    String myJSON;

    private static final String TAG_RESULTS="result";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "nombre";
    private static final String TAG_ADD ="telefono";
    private Handler mHandler;
    private int mInterval = 8000; // 5 seconds by default, can be changed later


    JSONArray peoples = null;

    ArrayList<HashMap<String, String>> personList;

    ListView list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.listView);
        personList = new ArrayList<HashMap<String,String>>();
        mHandler = new Handler();
        startRepeatingTask();
        getData();



    }



// METODO QUE MUESTRA LA LISTA O LA MATRIZ
    protected void showList(){
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String address = c.getString(TAG_ADD);

                HashMap<String,String> persons = new HashMap<String,String>();

                persons.put(TAG_ID,id);
                persons.put(TAG_NAME,name);
                persons.put(TAG_ADD,address);

                personList.add(persons);
            }

            ListAdapter adapter = new SimpleAdapter(
                    VerA.this, personList, R.layout.list_item,
                    new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                    new int[]{R.id.id, R.id.name, R.id.address}
            );

            list.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


///METODO QUE EXTRAE LOS DATOS DE EL JASON PHP
    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://192.168.1.108/selectAllJSON.php");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {

                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }


/// METODO PARA HACER QUE SE EJECUTE LA CONSULTA A LA BD CADA 8 SEGUNDOS, EL QUE NO FUNCIONA 
    Runnable mStatusChecker = new Runnable() {
        @Override
        public void run() {
            getData();
            mHandler.postDelayed(mStatusChecker, mInterval);
        }
    };
    void startRepeatingTask() {
        mStatusChecker.run();
    }
}