首先感谢帮助我!
我想在我的android项目中做一个MCQ,我可以加载问题,但我无法加载一个选项。为了成功加载我的一个选择,我必须评论我的:
echo json_encode(array("lesQuestions"=>$lesQuestions));
在我的PHP中。
对我而言,我无法从我的php访问我的第二个JSON,我不知道为什么!
这是我的JSON回复:
{
"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"
}
}
}
这是我的PHP,我得到了一个JSONObject:
$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));
?>
这是我的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);
我希望你能帮助我,因为我在这2天遇到了这个问题,我无法解决它!
再见!