android使用php,mcq应用程序获取数据。无法逐一检索

时间:2014-04-14 06:51:59

标签: php android json

我正在制作一个基本上是mcq问题答案测试的android应用程序。 我在php中有一个数据库,我使用json来获取php中的数据。

我可以在后台使用异步任务获取json对象中的所有问题并启动一个新线程来打印它,但是当我尝试在我的活动中打印它时,它会直接打印最后一个问题。

提前感谢你。

这对我来说意义重大

我会附上我的源代码。这只是主要部分

类LoadQuestion扩展了AsyncTask {

    @Override
    protected String doInBackground(String... args) {

        // TODO Auto-generated method stub
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();



//      Log.e("no of questions",noofq);
        Log.e("list name ", "abc");
    //  params.add(new BasicNameValuePair(TAG_NAME, name));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_get_question, "POST",
                params);
        // Check your log cat for JSON reponse
        Log.e("All questions ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                Log.e("success", "success");

                // questions found
                // Getting Array of question
                questions = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < questions.length(); i++) {
                    JSONObject c = questions.getJSONObject(i);
                    Log.e("in for", "success");
                    // Storing each json item in variable
                    String qno = c.getString(TAG_PID);
                    final String question = c.getString(TAG_NAME);
                    final String opt1 = c.getString(TAG_OP1);
                    final String opt2 = c.getString(TAG_OP2);
                    final String opt3 = c.getString(TAG_OP3);
                    final String opt4 = c.getString(TAG_OP4);
                    final String ans = c.getString(TAG_ANS);
                //  final String no=c.getString(TAG_NO);
                    Log.e("string", "success " + qno + " " + question + " "
                            + opt1 + opt2 + opt3 + opt4 + ans);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    Log.e("hash map", "success");

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, qno);
                    map.put(TAG_NAME, question);
                    map.put(TAG_OP1, opt1);
                    map.put(TAG_OP2, opt2);
                    map.put(TAG_OP3, opt3);
                    map.put(TAG_OP4, opt4);

                    // adding HashList to ArrayList
                    questionlist.add(map);

                    Handler refresh = new Handler(Looper.getMainLooper());
                    refresh.post(new Runnable() {
                        public void run() {
                            try {
                                Log.e("hello","hello");

                                e1 = (TextView) findViewById(R.id.textView1);
                                e1.setText(question);
                                r1 = (RadioButton) findViewById(R.id.radioButton1);
                                r2 = (RadioButton) findViewById(R.id.radioButton2);
                                r3 = (RadioButton) findViewById(R.id.radioButton3);
                                r4 = (RadioButton) findViewById(R.id.radioButton4);
                                r1.setText(opt1);
                                r2.setText(opt2);
                                r3.setText(opt3);
                                r4.setText(opt4);
                                rg = (RadioGroup) findViewById(R.id.answers);
                                b1 = (Button) findViewById(R.id.finish);
                                b1.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View arg0) {
                                        // TODO Auto-generated method stub
                                        Log.e("hey","button");
                                        int selectedId = rg
                                                .getCheckedRadioButtonId();
                                        r5 = (RadioButton) findViewById(selectedId);
                                    //  String sans = r1.getText().toString();

                                        int id1,id2,id3,id4;
                                        id1=r1.getId();
                                        id2=r2.getId();
                                        id3=r3.getId();
                                        id4=r4.getId();

                                        //Log.e("abcd","abcd");
                                        int sid=r5.getId();
                                        Log.e("ans id "+id1,"selected id "+sid);
                                        int anss = Integer.parseInt(ans);

0 个答案:

没有答案