V Mobile
Kamis, 28 Juli 2016
How to send data to mysql from android app
Java
public void insertRecords(String a,String b,String c) { ArrayList<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>(); nameValuePairs.add(new BasicNameValuePair("a", a)); nameValuePairs.add(new BasicNameValuePair("b",b)); nameValuePairs.add(new BasicNameValuePair("c",c)); sendData(nameValuePairs); } public void sendData(ArrayList<namevaluepair> data) { InputStream is=null; try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/insert.php"); httppost.setEntity(new UrlEncodedFormEntity(data)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("pass 1", "connection success "); } catch(Exception e) { Log.e("Fail 1", e.toString()); Toast.makeText(getApplicationContext(), "Invalid IP Address", Toast.LENGTH_LONG).show(); } String result=""; try { BufferedReader reader = new BufferedReader (new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.r eadLine()) != null) { sb.append(line + " "); } is.close(); result = sb.toString(); Log.e("pass 2", "connection success "); } catch(Exception e) { Log.e("Fail 2", e.toString()); } }
and call insertRecords(products.get(i).name,products.get(i).price,products.get(i).quantity ) in your button action or where you you want , to add all data
connexion.php
<?php header(Content-type=application/json; charset=utf-8); $dns =mysql:host=localhost;dbname=test; //you data base name $user=root; $pw=; try { $cnx = new PDO($dns,$user,$pw); } catch (PDOException $e) { echo "Connexion imposible <br> "; echo $e->getMessage(); die(); } ?>
insert.php
<?php require_once("class-insert.php"); $a = isset($_POST[a]) ? $_POST[a] : ; $b = isset($_POST[b]) ? $_POST[b] : ; $c = isset($_POST[c]) ? $_POST[c] : ; $obj=new commande($a,$b,$c); $obj->add($obj); ?>
class-insert.php
<?php require_once("connexion.php"); class commande{ private $name; private $price; private $quantity; //________________________________________________________________ function __construct($name,$price,$quantity) { $this->name=$name; $this->price=$price; $this->quantity=$quantity; //_______________________________________________________________ } function add() { global $cnx; $sql="insert into commande values(".$this->name.",".$this->price.",".$this->quantity.")"; if($cnx->exec($sql)) echo" succes:"; else echo"problem"; } } ?>
How to get data from MySQL database to Android app
Tidak ada komentar:
Posting Komentar
Posting Lebih Baru
Posting Lama
Beranda
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar