android - Arraylist 未正确解析

标签 android mysql arraylist

您好,我遇到了一个奇怪的问题。我正在尝试使用 mysql 数据库中的问题和答案制作一个测验应用程序。我按照它们看起来的样子解析值 here .

我如何创建 List:

@Override
    protected List<QuestionsList> doInBackground(String... params) {
        nameValuePairs = new ArrayList<>();
        try {
            url = new URL(params[0]);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setConnectTimeout(15000);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            setupDataToDB();
            outputStream = httpURLConnection.getOutputStream();
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            bufferedWriter.write(StringGenerator.queryResults(nameValuePairs));
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            httpURLConnection.connect();
            inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
            jsonResult = StringGenerator.inputStreamToString(inputStream, QuestionsActivity.this);
            jsonResponse = new JSONObject(jsonResult.toString());
            Log.e("Response: ", jsonResult.toString());
            checkDisplayLanguage(langText);
            questionsLists = new ArrayList<>();
            for (int i = 0; i < jsonMainNode.length(); i++) {
                jsonChildNode = jsonMainNode.getJSONObject(i);
                questionName = jsonChildNode.optString(Constants.QUESTION_NAME_JSON_NAME);
                Log.e("Question Name: ", questionName);
                jsonArray = new JSONArray(jsonChildNode.optString(Constants.QUESTIONS_ANSWERS_ARRAY));
                for (int j = 0; j < jsonArray.length(); j++) {
                    jsonSecondChildNode = jsonArray.getJSONObject(j);
                    answer1 = jsonSecondChildNode.optString("answer1");
                    answer2 = jsonSecondChildNode.optString("answer2");
                    answer3 = jsonSecondChildNode.optString("answer3");
                    iscorrect1 = jsonSecondChildNode.optString("iscorrect1");
                    iscorrect2 = jsonSecondChildNode.optString("iscorrect2");
                    iscorrect3 = jsonSecondChildNode.optString("iscorrect3");
                    question_answers = new ArrayList<>();
                    question_answers.add(answer1);
                    question_answers.add(answer2);
                    question_answers.add(answer3);
                    question_iscorrect = new ArrayList<>();
                    question_iscorrect.add(iscorrect1);
                    question_iscorrect.add(iscorrect2);
                    question_iscorrect.add(iscorrect3);
                    Log.e("Answers in for loop", question_answers.toString());
                    questionsLists.add(new QuestionsList(questionName, answersArray, question_iscorrect));
                }

            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return questionsLists;
    }

后执行看起来像这样:

@Override
        protected void onPostExecute(List<QuestionsList> lists) {
            super.onPostExecute(lists);
            pDialog.dismiss();
            position = 0;
            question.setText(lists.get(position).getName());
            answersArray = lists.get(position).getAnswers();
            ans1 = answersArray.get(position);
            ranswer1.setText(ans1);
        }

调试信息如下所示:

E/Question Name:: Where to look to find journal articles
E/Answers in for loop: [In the librarys catalog , , ]
E/Answers in for loop: [, In alphabetical list of healink , ]
E/Answers in for loop: [, , Databases available in the library's site]
E/Question Name:: What information we provide magazine
E/Answers in for loop: [Published research experiments current information, , ]
E/Answers in for loop: [, Lists information about people, addresses, organizations, ]
E/Answers in for loop: [, , Legislation, competitions]
E/Question Name:: What is the E/Answers in for loop: [Is the number used for the registration of periodical publications, , ]
E/Answers in for loop: [, Is the International Unique number used for registration of printed books, ]
E/Answers in for loop: [, , Is the International Unique number used for the recording of Publications mixed forms]

我在这里做错了什么,arraylist 没有正确解析?

最佳答案

这是某种拼写错误或其他错误,需要调试并慢慢解决,而不是 StackOverflow。

我个人认为您的 View (保存在 answer1、answer2 和 answer3 中的 View )都是同一个 View ,因为初始化 Activity 时存在错误。但坦率地说,这个问题应该关闭,因为它没有显示出具体问题。

此外,添加:

question_answers = new HashMap<String,String>();

行前:

question_answers.put(Constants.FIRST_ANSWER_POST_NAME, answer1);

关于android - Arraylist 未正确解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36227024/

相关文章:

具有内连接的 MySql 存储过程

java - 将字符串和数字与android中的列表分开

java - 删除元素时 ArrayList hasNext 的行为

android - 如何在cordova中使用genymotion?

java - Oreo 中我自己的 URI 上的 ContentProvider.notifyChange 的安全异常

MySQL,如何使用 PHP 验证字符串日期?

MySQL插入: Race condition

java - 从 java 类返回一个数组

java - 在 Java 中返回数组

android - Android 应用程序可以在运行时请求额外的权限吗?