Bonjour à tous
Je galère à récupérer mes données issu de mon service web en PHP depuis uen Activity Android:
J'arrive bien à récupérer les données "error", "explain" et "code" mais pour ce qui est de "data", je ne vois pas comment faire. Je souhaite boucler sur les éléments pour les afficher dans une listview. Mon problème se situe dans "case "TOTO_GETLIST_01_OK":"
Code : Sélectionner tout - Visualiser dans une fenêtre à part {"error":false,"data":{"1":{"nom":"Titi","prenom":"Toto","mail":"Tata","id":"1"},"2":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"2"},"3":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"3"},"4":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"4"},"5":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"5"},"6":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"6"},"7":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"7"},"8":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"8"},"9":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"9"},"10":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"10"},"11":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"11"},"12":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"12"},"13":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"13"},"14":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"14"},"15":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"15"},"16":{"nom":"addUser","prenom":"addUser","mail":"addUser","id":"16"}},"explain":"Enregistrements trouv?s","code":"TOTO_GETLIST_01_OK"}
Je ne sais pas si le JSON est sur un format idéal et/ou si je dois utiliser JSONObject ou JSONArray. Pouvez vous m'aider ?
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 try { JSONObject jObj = new JSONObject(response); Boolean isError = valueOf(jObj.getString("error")); String explain = jObj.getString("explain"); String Code_Ret = jObj.getString("code"); if (isError) { Toast.makeText(getApplicationContext(), Code_Ret + " : " + explain , Toast.LENGTH_LONG).show(); } else { switch (Code_Ret) { case "TOTO_GETLIST_01_OK": JSONArray data = jObj.getJSONArray("data"); for (int i = 0; i < data.length(); i++) { JSONObject item = data.getJSONObject(i); Integer id = item.getInt("id"); String nom = item.getString("nom"); String prenom = item.getString("prenom"); String mail = item.getString("mail"); Toto totoJSON = new Toto(id,nom,prenom,mail); } break; case "TOTO_GETLIST_02_EMPTY": Toast.makeText(getApplicationContext(), explain, Toast.LENGTH_LONG).show(); break; default: Toast.makeText(getApplicationContext(), Code_Ret + " : " + "Erreur du service inconnue", Toast.LENGTH_LONG).show(); break; } } } catch (JSONException e) { // JSON error e.printStackTrace(); Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); }
Partager