je besoin d'aide s'il vous plait
j'arrive pas à récupérer les données du base de données
pour le code php!ça marche bien !! mais en java il y a un problème dans le code
json.getString("success") != nul
bon j'ai utiliser JSONParser comme classe qui avec une fonction qui retourne un JSONObject puisque mon json est sous la forme{success=1,etat=en cours de traitement....} j'ai pas besion d'un JSONarray.
et là je ne sais pas c'est quoi le probléme?
voici le code JSONParser
voici le code php
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; JSONArray jArray; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); Log.e("JSON", json); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { //jArray = new JSONArray(json); // jObj = jArray.getJSONObject(0); jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } }
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 <?php if (isset($_POST['tag']) && $_POST['tag'] != '') { // get tag $tag = $_POST['tag']; // include db handler require_once 'fonction.php'; $link = mysql_connect("localhost","root","") or die ("Impossible de se connecter"); mysql_select_db ("bdcni") or die ("Impossible d'accéder à la base de données"); $db = new DB_Functions(); // response Array $response = array("tag" => $tag, "success" => 0, "error" => 0); if ($tag == 'cv') { // Request type is Register new user $numcin = $_POST['numcin']; $textnom = $_POST['nom']; $textprenom = $_POST['prenom']; $adresse = $_POST['adresse']; $nationalité = $_POST['nationalité']; $adrmail = $_POST['adrmail']; $textfrm = $_POST['textfrm']; $textexp = $_POST['textexp']; $textSE = $_POST['textSE']; $textBD = $_POST['textBD']; $textDEV = $_POST['textDEV']; $textMDD = $_POST['textMDD']; $id_sujet = $_POST['id_sujet']; // check if user is already existed if ($db->isUserExisted($numcin)) { // user is already existed - error response $response["error"] = 2; //$response["error_msg"] = "stagiaire existe déja"; echo json_encode($response); } else { // ajouter stagiaire $user = $db->add_stagiaire( $numcin,$textnom,$textprenom, $adresse, $nationalité,$adrmail,$textfrm,$textexp,$textSE,$textBD,$textDEV,$textMDD,$id_sujet); if ($user) { echo"ajouté"; // user add successfully $response["success"] = 1; $response["error"] = 0; //$response["error_msg"] = "votre demande est envoyé avec succées"; echo json_encode($response); } else { // user failed to store echo"erreur"; $response["error"] = 1; //$response["error_msg"] = "erreur de CONNEXION "; echo json_encode($response); } } }else if ($tag =='compte') { // Request type is Register new user $numcin = $_POST['numcin']; $user = $db->getuserbycin($numcin); if ($user) { $response["success"] = 1; $response["etat"] = $user["etat"]; $response["decision"] = $user["decision"]; $response["nom_stagiaire"] = $user["nom_stagiaire"]; $response["prenom_stagiaire"] = $user["prenom_stagiaire"]; $response["nom_encadreur"]= $user["nom"]; $response["prenom_encadreur"] = $user["prenom"]; $response["titre_sujet"] = $user["titre"]; $response["date_debut"] = $user["date_debut"]; $response["date_fin"] = $user["date_fin"]; //echo json_encode($response); //$response["error"] = 0; //$response["error_msg"] = "votre demande est envoyé avec succées"; echo json_encode($response); } else { $response["error"] = 2; //$response["error_msg"] = "vous n'etes pas enregistré ou numéro de cin incorrect!"; echo json_encode($response);} } else { echo "Invalid Request"; } } else { echo "Access Denied"; } ?>
et voici le code java main
si vous plait je besoin d'aide!! merci d'avance
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.param_layout); userFunction = new UserFonctions(); cin = (EditText)findViewById(R.id.password); ErrorMsg= (TextView)findViewById(R.id.erreurtext); final String numcin= cin.getText().toString(); Button ok = (Button)findViewById(R.id.button3); ok.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { JSONObject json =userFunction.getUser(cin.getText().toString()); try { if (json.getString("success") != null) { ErrorMsg.setText(""); String res = json.getString("success"); if(Integer.parseInt(res) == 1){ String etat = json.getString("etat"); String decision = json.getString("decision"); String nom_stagiaire = json.getString("nom_stagiaire"); String prenom_stagiaire = json.getString("prenom_stagiaire"); String nom_encadreur = json.getString("nom_encadreur"); String prenom_encadreur = json.getString("prenom_encadreur"); String titre = json.getString("titre_sujet"); String date_debut = json.getString("date_debut"); String date_fin= json.getString("date_fin"); alert = new AlertDialog.Builder(paramActivity.this); alert.setTitle("Informations sur la ville"); alert.setIcon(R.drawable.home); alert.setMessage("ggg"); alert.setPositiveButton("Ok", null); alert.show(); }else{ // Error in login ErrorMsg.setText("Incorrect username/password"); } } } catch (JSONException e) { e.printStackTrace(); } } }); } } aussi la fonction getUser dans la classe userFonctions c'est comme suit: public JSONObject getUser(String cin){ // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("tag", "compte")); params.add(new BasicNameValuePair("numcin",cin )); // getting JSON Object JSONObject json = jsonParser.getJSONFromUrl(registerURL, params); // return json return json; }![]()
Partager