bonjour ,
je veux envoyer un Array de JAVA (Android) vers PHP ( serveur ) à l'aide de JSONArray .
j'ai trouvé nombreux tutoriel mais ça marche pas, je sais pas pourquoi
surement je fais une erreur du coté php,
j'ai trouvé cette solution mais il parle pas du coté php
voila mon code :
php code :
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 JSONObject jobj = null; JSONArray jArray = new JSONArray(); while (cursor.moveToNext()) { String minute=cursor.getString(1); String j1=cursor.getString(2); String j2=cursor.getString(3); //..... try { jobj.put("j1", j1); jobj.put("j2", j2); //...... jobj.put("userid",id); // Log.i("json sync",jobj.toString()); jArray.put(jobj); } catch (JSONException e) { e.printStackTrace(); } } cursor.close(); Log.i("json array",jArray.toString()); InputStream is = null; String result = null; int TIMEOUT_MILLISEC=30000; // Envoie de la commande http try { HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit HttpResponse response; JSONObject json = new JSONObject(); String url = "http://exemple.php"; HttpPost post = new HttpPost(url); StringEntity se = new StringEntity( jArray.toString()); se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); response = client.execute(post); HttpEntity entity = response.getEntity(); is = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } result = sb.toString(); is.close(); Log.i("test ", result.toString()); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); }
Edition 1:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 $json = file_get_contents('php://input'); $obj = json_decode($json); $output=$obj; while ($val=$obj->fetch()) { $heure = $val["minute"]; $id_client =$val['userid']; $req = $bdd->exec( //... //........ } print(json_encode($output,JSON_UNESCAPED_UNICODE)); mysql_close();
j'ai trouvé l'erreur c'était dans la boucle while ... je l'ai remplacé par foreach
mais j'ai un autre probléme : l'execution des requetes se plante et l'erreur suivant 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
45
46
47
48
49 $json = file_get_contents('php://input'); $obj = json_decode($json); foreach($obj as $adresse){ $heure = $adresse -> {"minute"}; $id_client = $adresse -> {"userid"}; $j1 = $adresse -> {"j1"}; $j2 = $adresse -> {"j2"}; $j3 = $adresse -> {"j3"}; $j4 = $adresse -> {"j4"}; $j5 = $adresse -> {"j5"}; $j6 = $adresse -> {"j6"}; $j7 = $adresse -> {"j7"}; $bdd->exec( 'UPDATE planning1 SET j1 = \'' . $j1 . '\' WHERE (planning1.id_client = \'' . $id_client . '\') AND (planning1.minute ='.$heure.')'); //$bdd->closeCursor(); $output['j1']=true; $bdd->exec( 'UPDATE planning1 SET j2 = \'' . $j2 . '\' WHERE (planning1.id_client = \'' . $id_client . '\') AND (planning1.minute = '.$heure.')'); //$bdd->closeCursor(); $output['j2']=true; $bdd->exec( 'UPDATE planning1 SET j3 = \'' . $j3 . '\' WHERE (planning1.id_client = \'' . $id_client . '\') AND (planning1.minute = '.$heure.')'); //$bdd->closeCursor(); $output['j3']=true; $bdd->exec( 'UPDATE planning1 SET j4 = \'' . $j4 . '\' WHERE (planning1.id_client = \'' . $id_client . '\') AND (planning1.minute = '.$heure.')'); //$bdd->closeCursor(); $output['j4']=true; $bdd->exec( 'UPDATE planning1 SET j5 = \'' . $j5 . '\' WHERE (planning1.id_client = \'' . $id_client . '\') AND (planning1.minute = '.$heure.')'); //$bdd->closeCursor(); $output['j5']=true; $bdd->exec( 'UPDATE planning1 SET j6 = \'' . $j6 . '\' WHERE (planning1.id_client = \'' . $id_client . '\') AND (planning1.minute = '.$heure.')'); //$bdd->closeCursor(); $output['j6']=true; $bdd->exec( 'UPDATE planning1 SET j7 = \'' . $j7 . '\' WHERE (planning1.id_client = \'' . $id_client . '\') AND (planning1.minute = '.$heure.')'); //$bdd->closeCursor(); $output['j7']=true; }
Aidez moi SVP
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8The server encountered an internal error or misconfiguration and was unable to complete your request.<P> Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.<P> More information about this error may be available in the server error log.<P>
Partager