lunedì 2 aprile 2012

Connettersi a MySql da Android


Android ha varie possibilita' di salvare dati persistenti ma non c'e' una via semplice per connettersi a database esterni in particolare MySql
Usando JDBC ho avuto diversi problemi (il sorgente si compila bene ma non riesce a connettersi) per cui un sistema alternativo e' quello di passare per un intermediario dato da uno script in PHP
per prima cosa si deve impostare il permesso


<uses-permission android:name="android.permission.INTERNET"></uses-permission>

il server e' 192.168.0.1 e lo script si trova in http://192.168.0.1/zenith/insert.php
vengono passate due variabili (piatto e tavolo) popolate

-------------------------------------------------------------------------------------------


               InputStream is;
        String piatto = spin.getSelectedItem().toString();
//String tavolo = tav_txt.getText().toString();
        String tavolo = spin_tav.getSelectedItem().toString();

        ArrayList <NameValuePair> nameValuePairs = new ArrayList <NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("piatto",piatto));
        nameValuePairs.add(new BasicNameValuePair("tavolo",tavolo));
       
        try{
        HttpClient httpclient = new DefaultHttpClient();
       
        HttpPost httppost = new HttpPost("http://192.168.0.1/zenith/insert.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.d("Zenith","Dato salvato");
           Toast.makeText( getApplicationContext(),R.string.riuscito,Toast.LENGTH_SHORT ).show();
               // suona per conferma
               Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
               Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
               r.play();


        }
        catch(Exception e)
        {
           Toast.makeText( getApplicationContext(),R.string.non_riuscito,Toast.LENGTH_SHORT ).show();
        Log.e("Zenith", "Error in http connection"+e.toString());
        }
-------------------------------------------------------------------------------------------
la struttura del database Mysql e' data dal codice

-------------------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `comande` (


  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `tavolo` int(11) NOT NULL,
  `piatto` text NOT NULL,
  `data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;

-------------------------------------------------------------------------------------------

lo script Php insert.php invece risulta essere

-------------------------------------------------------------------------------------------
<?


$hostname_localhost ="localhost";
$database_localhost ="zenith";
$username_localhost ="xxxxxxxxx";
$password_localhost ="xxxxxxxxxxx";
var_dump($_REQUEST); 
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db("zenith");
$sql_string = "INSERT INTO comande (id,tavolo,piatto,data) VALUES (NULL,'".$_REQUEST['tavolo']."','".$_REQUEST['piatto']."',CURRENT_TIMESTAMP)";
$sql=mysql_query($sql_string);
mysql_close();
?>




Nessun commento:

Posta un commento

SV305 raw image

Sto provando a prendere il controllo della SVBony SV305 senza passare da AstroDMX La camera puo' essere controllata tramite l'SDK ch...