dongmiyu8979 2016-04-28 12:03
浏览 39

无法读取我的第二个JSON ANDROID

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 !

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用