我如何从android服务器中获取数据

时间:2018-07-16 11:47:29

标签: android retrofit2

我想从data_format_value中获取数据。它仅返回最后一个值。我不知道错误发生在哪里。我想获取所有的data_format_value。

{
    "org_id": 1
    "test_instance_id": 237,
    "section_attributes": [
        {
            "section_id": 1,
            "section_name": "Section-A",
            "section_status": "Started",
            "item_count": 5
        }
    ],
    "itemset_sections": [
        {
            "section_id": 1,
            "section_name": "Section-A",
            "timer": 30,
            "section_items": [
                {
                    "item_id": 190,
                    "item_type": 1,
                    "is_directive": 0,
                    "directive_content": {},
                    "hints": "scientists",
                    "item": [
                        {
                            "data_id": 877,
                            "data_format_id": 1,
                            "data_format_value": "Why haven't Indian scientists made such headway in any field after independence ?",
                            "item_df_sequence": 1,
                            "data_identifier": null
                        },
                        {
                            "data_id": 878,
                            "data_format_id": 1,
                            "data_format_value": "Indian scientists are not provided with up to date laboratory facilities.",
                            "item_df_sequence": 2,
                            "data_identifier": null
                        },
                        {
                            "data_id": 879,
                            "data_format_id": 1,
                            "data_format_value": "Indian scientists regard that knowledge of western science advances is enough for a nation to advance.",
                            "item_df_sequence": 3,
                            "data_identifier": null
                        }
                    ],
                    "answer_choices": [
                        {
                            "correct_answer": false,
                            "answer_choice_id": 4,
                            "choice_elements": [
                                {
                                    "data_id": 883,
                                    "data_format_id": 1,
                                    "data_format_value": "data given in both statements I and II together are not sufficient to answer the question. ",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 1
                        },
                        {
                            "correct_answer": false,
                            "answer_choice_id": 2,
                            "choice_elements": [
                                {
                                    "data_id": 881,
                                    "data_format_id": 1,
                                    "data_format_value": "data in statement II alone are sufficient to answer the question.",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 2
                        },
                        {
                            "correct_answer": true,
                            "answer_choice_id": 1,
                            "choice_elements": [
                                {
                                    "data_id": 880,
                                    "data_format_id": 1,
                                    "data_format_value": "data in statement I alone are sufficient to answer the question.",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 3
                        },
                        {
                            "correct_answer": false,
                            "answer_choice_id": 3,
                            "choice_elements": [
                                {
                                    "data_id": 882,
                                    "data_format_id": 1,
                                    "data_format_value": "data either in statement I alone or in statement II alone are sufficient to answer the question.",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 4
                        }
                    ],
                    "Answer_Choice_Type": "Single Answer",
                    "score_type": "Item Lvl Score",
                    "ItemSet_Item_Key": 273,
                    "yet_to_visit": 0,
                    "filterCategory": "red",
                    "mark_for_review": 0,
                    "not_answered": 1,
                    "answered": 0,
                    "answered_marked_for_review": 0,
                    "seq_no": 1,
                    "hints_shown": "n",
                    "user_selected_option": []
                },
                {
                    "item_id": 193,
                    "item_type": 1,
                    "is_directive": 0,
                    "directive_content": {},
                    "hints": "synthesised",
                    "item": [
                        {
                            "data_id": 897,
                            "data_format_id": 1,
                            "data_format_value": "Which of the following is the newest element to be discovered and synthesised?",
                            "item_df_sequence": 1,
                            "data_identifier": null
                        },
                        {
                            "data_id": 4,
                            "data_format_id": 11,
                            "data_format_value": "https://awspikaresources.s3.ap-south-1.amazonaws.com/TenantsDetails/Tech4india2018-06-16/TenantFilesContent/Videos/61e0158cde97a6f2e3a9f3a7c5d81ccf1530684346.mp4",
                            "item_df_sequence": 2,
                            "data_identifier": null
                        }
                    ],
                    "answer_choices": [
                        {
                            "correct_answer": false,
                            "answer_choice_id": 1,
                            "choice_elements": [
                                {
                                    "data_id": 898,
                                    "data_format_id": 1,
                                    "data_format_value": "Flerovium (114)",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 1
                        },
                        {
                            "correct_answer": true,
                            "answer_choice_id": 4,
                            "choice_elements": [
                                {
                                    "data_id": 901,
                                    "data_format_id": 1,
                                    "data_format_value": "Ununseptium (117)",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 2
                        },
                        {
                            "correct_answer": false,
                            "answer_choice_id": 2,
                            "choice_elements": [
                                {
                                    "data_id": 899,
                                    "data_format_id": 1,
                                    "data_format_value": "Ununpentium (115)",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 3
                        },
                        {
                            "correct_answer": false,
                            "answer_choice_id": 3,
                            "choice_elements": [
                                {
                                    "data_id": 900,
                                    "data_format_id": 1,
                                    "data_format_value": "Livermorium (116)",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 4
                        }
                    ],
                    "Answer_Choice_Type": "Single Answer",
                    "score_type": "Item Lvl Score",
                    "ItemSet_Item_Key": 275,
                    "yet_to_visit": 1,
                    "filterCategory": "gray",
                    "mark_for_review": 0,
                    "not_answered": 0,
                    "answered": 0,
                    "answered_marked_for_review": 0,
                    "seq_no": 3,
                    "hints_shown": "n",
                    "user_selected_option": []
                },
                {
                    "item_id": 195,
                    "item_type": 2,
                    "is_directive": 0,
                    "directive_content": {},
                    "hints": "anti-tetanus ",
                    "item": [
                        {
                            "data_id": 904,
                            "data_format_id": 1,
                            "data_format_value": "Blueberries cost more than strawberries",
                            "item_df_sequence": 1,
                            "data_identifier": null
                        },
                        {
                            "data_id": 905,
                            "data_format_id": 1,
                            "data_format_value": "Blueberries cost less than raspberries.",
                            "item_df_sequence": 2,
                            "data_identifier": null
                        },
                        {
                            "data_id": 906,
                            "data_format_id": 1,
                            "data_format_value": "Raspberries cost more than strawberries and blueberries",
                            "item_df_sequence": 3,
                            "data_identifier": null
                        },
                        {
                            "data_id": 907,
                            "data_format_id": 1,
                            "data_format_value": "If the first two statements are true, the third statement is",
                            "item_df_sequence": 4,
                            "data_identifier": null
                        }
                    ],
                    "answer_choices": [
                        {
                            "data_id": 8,
                            "data_format_id": 1,
                            "data_format_value": "True",
                            "answer_choice_id": 1,
                            "item_df_sequence": 1,
                            "data_identifier": null,
                            "correct_answer": true,
                            "choosed_answer": false,
                            "seq_id": 1
                        },
                        {
                            "data_id": 9,
                            "data_format_id": 1,
                            "data_format_value": "False",
                            "answer_choice_id": 2,
                            "item_df_sequence": 2,
                            "data_identifier": null,
                            "correct_answer": false,
                            "choosed_answer": false,
                            "seq_id": 2
                        }
                    ],
                    "Answer_Choice_Type": "Single Answer",
                    "score_type": "Item Lvl Score",
                    "ItemSet_Item_Key": 277,
                    "yet_to_visit": 1,
                    "filterCategory": "gray",
                    "mark_for_review": 0,
                    "not_answered": 0,
                    "answered": 0,
                    "answered_marked_for_review": 0,
                    "seq_no": 4,
                    "hints_shown": "n",
                    "user_selected_option": []
                },
                {
                    "item_id": 191,
                    "item_type": 1,
                    "is_directive": 0,
                    "directive_content": {},
                    "hints": "segment",
                    "item": [
                        {
                            "data_id": 884,
                            "data_format_id": 1,
                            "data_format_value": "Suppose we have an image given below",
                            "item_df_sequence": 1,
                            "data_identifier": null
                        },
                        {
                            "data_id": 7,
                            "data_format_id": 6,
                            "data_format_value": "https://awspikaresources.s3.ap-south-1.amazonaws.com/TenantsDetails/Tech4india2018-06-16/TenantFilesContent/Images/87a95c70ae73d23f351b50cddcf459641530684578.jpeg",
                            "item_df_sequence": 2,
                            "data_identifier": null
                        },
                        {
                            "data_id": 885,
                            "data_format_id": 1,
                            "data_format_value": "Our task is to segment the objects in the image. A simple way to do this is to represent the image in terms of the intensity of pixels and the cluster them according to the values. On doing this, we got this type of structure",
                            "item_df_sequence": 3,
                            "data_identifier": null
                        },
                        {
                            "data_id": 8,
                            "data_format_id": 6,
                            "data_format_value": "https://awspikaresources.s3.ap-south-1.amazonaws.com/TenantsDetails/Tech4india2018-06-16/TenantFilesContent/Images/04c5c1be6bbfd4f44773e4c68b84c05d1530684594.jpeg",
                            "item_df_sequence": 4,
                            "data_identifier": null
                        },
                        {
                            "data_id": 886,
                            "data_format_id": 1,
                            "data_format_value": "Suppose we choose k-means clustering to solve the problem, what would be the appropriate value of k from just a visual inspection of the intensity graph?",
                            "item_df_sequence": 5,
                            "data_identifier": null
                        }
                    ],
                    "answer_choices": [
                        {
                            "correct_answer": true,
                            "answer_choice_id": 3,
                            "choice_elements": [
                                {
                                    "data_id": 889,
                                    "data_format_id": 1,
                                    "data_format_value": "3",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 1
                        },
                        {
                            "correct_answer": false,
                            "answer_choice_id": 1,
                            "choice_elements": [
                                {
                                    "data_id": 887,
                                    "data_format_id": 1,
                                    "data_format_value": "1",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 2
                        },
                        {
                            "correct_answer": false,
                            "answer_choice_id": 2,
                            "choice_elements": [
                                {
                                    "data_id": 888,
                                    "data_format_id": 1,
                                    "data_format_value": "2",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 3
                        },
                        {
                            "correct_answer": false,
                            "answer_choice_id": 4,
                            "choice_elements": [
                                {
                                    "data_id": 890,
                                    "data_format_id": 1,
                                    "data_format_value": "4",
                                    "item_df_sequence": 1,
                                    "data_identifier": null
                                }
                            ],
                            "choosed_answer": false,
                            "seq_id": 4
                        }
                    ],
                    "Answer_Choice_Type": "Single Answer",
                    "score_type": "Item Lvl Score",
                    "ItemSet_Item_Key": 274,
                    "yet_to_visit": 1,
                    "filterCategory": "gray",
                    "mark_for_review": 0,
                    "not_answered": 0,
                    "answered": 0,
                    "answered_marked_for_review": 0,
                    "seq_no": 5,
                    "hints_shown": "n",
                    "user_selected_option": []
                }
            ]
        }
    ]
}

我也创建了pojo。 我的Android代码受到打击。在这里,我附加了我的android函数而没有pojo。 logcat中打印的数据不会显示在textview中

 private void fetchTestData() {

        JsonObject gsonObject = new JsonObject();

        JsonObject paramObject = new JsonObject();
        JsonObject current_section = new JsonObject();

        paramObject.addProperty("test_id", 30);
        paramObject.addProperty("user_id", 23);
        paramObject.addProperty("org_id", 1);
        paramObject.addProperty("schedule_id", 80);
        paramObject.addProperty("next_section_id","");
        paramObject.addProperty("group_id", "null");
        paramObject.addProperty("current_section", String.valueOf(current_section));

        JsonParser jsonParser = new JsonParser();
        gsonObject = (JsonObject) jsonParser.parse(paramObject.toString());

        retrofit2.Call<Test_Responce> userCall = api_interface.exampleresponce(gsonObject);
        userCall.enqueue(new Callback<Test_Responce>() {
            @Override
            public void onResponse(retrofit2.Call<Test_Responce> call, Response<Test_Responce> response) {

                if (response.isSuccessful()){
                    qnoPalette.clear();
                    countTimer = response.body().getTimer();
                    List<ItemsetSection>   getItemsetSection = response.body().getItemsetSections();
                    getSectionAttribute = response.body().getSectionAttributes();

                        //get Itemset Section
                        for (int i = 0;i<getItemsetSection.size();i++){

                            List<SectionItem>  getSection = getItemsetSection.get(i).getSectionItems();

                            for (int j = 0;j<getSection.size();j++){

                                 getItem = getSection.get(j).getItem();

                                for (int k=0;k<getItem.size();k++){

                                    Const.QuestionSize = getItem.size();

                                        strQuestion.add(getItem.get(k).getDataFormatValue());

                                }

                            }


                        }

                        for (int i =0; i<getSectionAttribute.size();i++){

                            txtQuestionPage.setText(Const.currentpage+"/"+getSectionAttribute.get(i).getItemCount());
                            sectionItem.add( getSectionAttribute.get(i).getSectionName());
                            Valuesfromserver();

                    }

                    Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
                    startCountDownTimer();
                }else {
                    Toast.makeText(getApplicationContext(),"else",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(retrofit2.Call<Test_Responce> call, Throwable t) {
                Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
                Log.e("error:",t.getMessage())
                ;            }
        });
    }

   btnQuestion.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        paletteLayout.setVisibility(View.GONE);
                        txtInst.setVisibility(View.GONE);

                        for(int i =0;i<strQuestion.size();i++{

                            textView.setText(strQuestion.get(i);

                        }

                       Log.e("questions", String.valueOf(strQuestion));
                    }
                });

仅返回最后一个问题

谢谢

1 个答案:

答案 0 :(得分:0)

for循环中出现问题。 我希望这段代码对您有用。

for (int i = 0; i <getSection.size();i++){
    getItem = getSection.get(i).getItem();
    for (int j = 0; j<getItem.size();j++){
        Const.QuestionSize = getItem.size();
        palette_model = new Palette_Model(getItem.get(j).getDataFormatValue(),
                getItem.get(j).getDataFormatId());
        strQuestion.add(palette_model);


    }

    Log.e("val", String.valueOf(strQuestion));

    qa.add(String.valueOf(strQuestion));
    strQuestion.clear();
}