2014-07-15 4 views
-2

Je sais qu'il ya quelques questions à ce sujet, mais je les lis et j'ai essayé le soluttion mais cela n'a pas fonctionné :(JSON à partir du fichier php tableau java

le script PHP donne ce tableau JSON résultat: les données [x] =

  ["alon","62","1.82","22","0","70","0","1"] 

(ce sont les données [x] variable)

je dois convertir ce résultat à Java variabls comme le nom, le poids, la taille etc .. mais je n » Je sais comment ..

s'il vous plaît aidez-moi

ma fonction:

 private class LongOperation extends AsyncTask<String, Void, Void> { 





     private final HttpClient Client = new DefaultHttpClient(); 
     private String Error = null; 

     protected void onPreExecute() { 


     } 

     protected Void doInBackground(String... urls) { 
      try { 

        HttpGet httpget = new HttpGet(urls[0]); 
       ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
       data[x] = Client.execute(httpget, responseHandler); 

      } catch (ClientProtocolException e) { 
       Error = e.getMessage(); 
       Toast.makeText(getApplicationContext(),"error2" , Toast.LENGTH_LONG).show(); 
       cancel(true); 
      } catch (IOException e) { 
       Error = e.getMessage(); 
       Toast.makeText(getApplicationContext(),"error34" , Toast.LENGTH_LONG).show(); 
       cancel(true); 
      } 

      return null; 
     } 

     public void onPostExecute(Void unused) { 

      String name = null,weight = null; 
      if (Error != null) { 
      } else { 

       // here I have to do something with the arrays... 

      Toast.makeText(getApplicationContext(),"d:" + data[x] + "o:" + name + " : " + weight, Toast.LENGTH_LONG).show(); 
      } 
      x++; 
     } 

    } 

Répondre

0

Créer une classe Modal pour cela.

class myModal { 
    private String name, weight, height, ...; 
    public String getName() { return this.name; } 
    public void setName(String name) { this.name = name; } 
    //and more getters and setters 
} 


JSONObject json = new JSONObject(data[x]); // in your sample its a JSONArray but its wrong formatted. make sure you encode it properply with php json_encode(array("data", yourdata))...); 

myModal modal = new myModal(); 
modal.setName(json.getString("name")); 

php doit être quelque chose comme

<?php 
    $data = array("name" => "myname", "weight" => 20); 
    print json_encode( $data ); 
?> 

alors que le JSON peut être analysé dans ce cas avec

JSONArray json = new JSONArray(data); 
for (int i = 0; i <= json.length();i++){ 
    JSONObject jsonObj = json.getJsonObject(i); 
    myModal modal = new myModal(); 
    modal.setString(jsonObj.getString("name")); 
    //and so on   
} 

assurez-vous de lire les bases de la compréhension

+0

J'ai essayé ce tu as dit. ne fonctionne toujours pas = [php montre ceci: ["data", {"name": "alon", "weight": "62", "height": "1.82", "age": "22", " semaines ":" 0 "," cible ":" 70 "," ketzev ":" 0 "," genre ":" 1 "}]. eclipse veut ajouter try and catch à votre code: JSONObject json = null; try { \t json = new JSONObject (données [x]); } catch (JSONException e) { \t // TODO Bloc de saisie généré automatiquement \t e.printStackTrace(); } myModal modal = new myModal(); try { \t modal.setName (json.getString ("nom")); } catch (JSONException e) { \t // TODO Bloc de saisie généré automatiquement \t e.printStackTrace(); } – Helena

+0

fichier php: \t \t $ data = array ("nom" => "$ name", "poids" => "$ poids", "height" => "$ height", "age" => "$ age "," semaines "=>" $ semaines "," target "=>" $ target "," ketzev "=>" $ ketzev "," genre "=>" $ genre "); \t \t $ length = nombre ($ array); \t \t echo json_encode (array ("données", $ données)); – Helena

+0

merci ça marche maintenant :) – Helena

Questions connexes