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++;
}
}
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
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
merci ça marche maintenant :) – Helena