IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Android Discussion :

Envoyer un JsonArray de JAVA Android vers PHP serveur et faire des update mysql


Sujet :

API standards et tierces Android

  1. #1
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2009
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Mai 2009
    Messages : 34
    Points : 54
    Points
    54
    Par défaut Envoyer un JsonArray de JAVA Android vers PHP serveur et faire des update mysql
    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 :
    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());
                    }
    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
    $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();
    Edition 1:

    j'ai trouvé l'erreur c'était dans la boucle while ... je l'ai remplacé par foreach
    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;
     
     
    }
    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
    The 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>
    Aidez moi SVP

  2. #2
    Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2009
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Mai 2009
    Messages : 34
    Points : 54
    Points
    54
    Par défaut
    le problème est résolu

    il fallait mettre une seul requête et remplacé le while par foreach et voila :

    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
     
    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 . '\', j2 = \'' . $j2 . '\', j3 = \'' . $j3 . '\', j4 = \'' . $j4 . '\', j5 = \'' . $j5 . '\', j6 = \'' . $j6 . '\', j7 = \'' . $j7 . '\' WHERE   (planning1.id_client = \'' . $id_client . '\') AND (planning1.minute ='.$heure.')');
    	//$bdd->closeCursor();
        $output=true; }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 31/05/2015, 10h15
  2. Réponses: 5
    Dernier message: 17/02/2015, 17h33
  3. Requête GET HTTP Android vers un serveur WEB
    Par sk310000 dans le forum Programmation système
    Réponses: 0
    Dernier message: 30/01/2015, 18h01
  4. Envoi de oordonnées à partir d'une application Android vers un serveur IIS
    Par saidasahraouiya dans le forum API standards et tierces
    Réponses: 7
    Dernier message: 16/01/2014, 17h27
  5. comment envoyer un fichier d'un client vers un serveur
    Par nad30 dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 22/05/2007, 13h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo