firstly thank for helping me !
I want to do a MCQ in my android project, i can load the question but i can't load one choice. To succesfully load one of my choice, i have to comment my :
echo json_encode(array("lesQuestions"=>$lesQuestions));
in my php.
To me, i can't acces my second JSON return from my php and i don't know why !
Here is my JSON return :
{
"lesQuestions": {
"1": {
"question": "Est-ce que Captain America gagne contre IronMan",
"id": "31"
}
}
} { //for JSONLint, this is where i have a problem
"lesChoix": {
"1": {
"choix": "Non"
},
"2": {
"choix": "Oui"
}
}
}
Here is my PHP and i get a JSONObject with it:
$db = mysqli_connect($host,$user,$pass,$db);
$questions = $db->query("SELECT question, id FROM question ORDER BY rand() LIMIT 1");
$iNb = 0 ;
while($row = mysqli_fetch_assoc($questions)){
$id=$row['id'];
$iNb = $iNb + 1;
$lesQuestions[$iNb] = $row;
}
echo json_encode(array("lesQuestions"=>$lesQuestions));
$choix = $db->query("SELECT choix FROM choix WHERE id_question = $id ORDER BY rand()");
$iNb2 = 0;
while ($row = mysqli_fetch_assoc($choix)) {
$iNb2 = $iNb2 + 1;
$lesChoix[$iNb2] = $row;
}
echo json_encode(array("lesChoix"=>$lesChoix));
?>
and here is my JAVA :
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://ent-ifsi.com/Projet/Application_Android/signeCliniques_android.php",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
try
{
JSONObject lesQuestions = response.getJSONObject("lesQuestions");
Iterator<?> keys = lesQuestions.keys();
JSONObject lesChoix = response.getJSONObject("lesChoix");
Iterator<?> keys1 = lesChoix.keys();
while(keys.hasNext()) {
String key = (String) keys.next();
if (lesQuestions.get(key) instanceof JSONObject) {
JSONObject obj = (JSONObject) lesQuestions.get(key);
String signesCliniques = obj.getString("question");
symptomesQuestions.setText(signesCliniques);
}
}
while (keys1.hasNext()) {
String key1 = (String) keys1.next();
if (lesChoix.get(key1) instanceof JSONObject) {
JSONObject obj1 = (JSONObject) lesChoix.get(key1);
String choix = obj1.getString("choix");
lesChoixButton.setText(choix);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e + "",Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.e("Volley", "ERROR");
Toast.makeText(getApplicationContext(), error +"",Toast.LENGTH_LONG).show();
}
}
);
queue.add(jsonObjectRequest);
I hope you can help me because i'm on this problem this 2 day and i can't solve it !
Bye !