How to get data from MySQL database to Android app Unknown الأحد، 12 أبريل 2015 label android label java label php Java private static class Product { private String name = "" ; private String price; private String quantity; public Product( String name, String price, String quantity ) { this.name = name ; this.price = price ; this.quantity = quantity ; } public static ArrayList<product> products; /** * Generate products. */ @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public static void generateProducts(){ String result = ""; InputStream isr = null; products=new ArrayList<product>(); try{ StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/getdata.php"); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); isr = entity.getContent(); } catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "utf-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } isr.close(); result=sb.toString(); } catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } //parse json data try { JSONArray jArray = new JSONArray(result); for(int i=0; i<jArray.length();i++){ JSONObject json = jArray.getJSONObject(i); products.add(new Product(json.getString("name"),Float.parseFloat(json.getString("price")), json.getString("quantity"))); } } catch (Exception e) { // TODO: handle exception Log.e("log_tag", "Error Parsing Data "+e.toString()); } } PHPconnexion.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(); } ?> getdata.php <?php require_once("connexion.php"); class prod{ private $name; private $price; private $quantity; function __construct($n,$p,$q) { $this->name=$n; $this->price=$p; $this->quantity=$q; } public static function liste() { global $cnx; $sql="select * from products"; $res=$cnx->query($sql); return($res->fetchAll(PDO::FETCH_OBJ)); } //**************************************************** } $liste=prod::liste(); print(json_encode($liste)); ?>How to send data to mysql from android app Share This : Unknown Related PostHow to send data to mysql from android appHow to get data from MySQL database to Android appHow to fix android cell phone won't restart after it got wet chevron_leftNext Endless - Responsive Admin Template chevron_rightPrevious How to send data to mysql from android app
comment 0 التعليقات:
more_vertsentiment_satisfied Emoticon