Bonjour,
Je débute avec android.
Je veux afficher le résultat d'une requête dans mon application. Je n'ai pas d'erreur dans mon code quand je compile ou que je l'exécute.
Quand je lance mon application rien ne s'affiche
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class MainActivity extends Activity { TextView txt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout rootLayout = new LinearLayout(getApplicationContext()); // txt = new TextView(getApplicationContext()); // rootLayout.addView(txt); setContentView(rootLayout); // txt.setText("Connexion..."); // txt.setText(postData()); // postData(); // } // public static final String strURL = "http://10.0.2.2/android/plat.php"; // public void postData() { // On créé un client http HttpClient httpclient = new DefaultHttpClient(); // On créé notre entête HttpPost httppost = new HttpPost("http://10.0.2.2/android/tablee/plat.php"); try { // On ajoute nos données dans une liste List nameValuePairs = new ArrayList(6); // On ajoute nos valeurs ici un identifiant et un message nameValuePairs.add(new BasicNameValuePair("test", "hu")); nameValuePairs.add(new BasicNameValuePair("message", "Android c'est coo!!!")); // Ajoute la liste à notre entête httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // On exécute la requête tout en récupérant la réponse HttpResponse response = httpclient.execute(httppost); // On peut maintenant afficher la réponse Log.e("http réponse",response.toString()); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } }.
Voici mon fichier plat.php :
Si dans ma machine je vais sur internet est je tape : http://10.0.2.2/android/plat.php , mon fichier fonctionne.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <?php mysql_connect("localhost","",""); mysql_select_db("plat"); $sql=mysql_query("SELECT * FROM plat"); while($row=mysql_fetch_assoc($sql)) $output[]=$row; print(json_encode($output)); mysql_close(); ?>
Partager