【问题标题】:How to get Json Array to work inside Asyntask - Android [duplicate]如何让 Json Array 在 Asynctask 中工作 - Android [重复]
【发布时间】:2013-07-06 17:04:22
【问题描述】:

我有一个 Asynctask,它在 doInBackground 部分中使用了 Json 函数。该函数收集一个 cmets 数组并将它们放入一个名为 KEY_COMMENTS 的变量中。在onPreExecute 中,它使用 for 循环将 cmets 放入 textView 以单独选择每个评论。问题是它没有选择每个评论,它只会选择一个。如果我将循环设置为超过 1 次,它将使应用程序崩溃。这是我的代码,

    class loadComments extends AsyncTask<JSONObject, String, JSONObject> {


            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);

            } 

            protected JSONObject doInBackground(JSONObject... params) {
            //do your work here

                JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);

                    return json2;



            }

            @Override
            protected void onPostExecute(JSONObject json2) {
                try {  
                    if (json2.getString(KEY_SUCCESS) != null) { 
                        registerErrorMsg.setText("");
                        String res2 = json2.getString(KEY_SUCCESS);
                        if(Integer.parseInt(res2) == 1){ 


                            JSONArray array = json2.getJSONArray(KEY_COMMENT);
                            for(int i = 0; i < 2; i++) {

                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                         commentBox.setBackgroundResource(R.drawable.comment_box_bg);
                                        layoutParams.setMargins(0, 10, 0, 10);
                                        commentBox.setPadding(0,0,0,10);
                                         commentBox.setOrientation(LinearLayout.VERTICAL);
                                         linear.addView(commentBox, layoutParams);


                                        commentBoxHeader.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                                         commentBoxHeader.setBackgroundResource(R.drawable.comment_box_bg);
                                        commentBoxHeader.setBackgroundResource(R.drawable.comment_box_header);
                                        commentBox.addView(commentBoxHeader);


                                        commentView.setText(array.getString(i));
                                        LinearLayout.LayoutParams commentViewParams = new LinearLayout.LayoutParams(
                                         LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                        commentViewParams.setMargins(20, 10, 20, 20);
                                        commentView.setBackgroundResource(R.drawable.comment_bg);
                                         commentView.setTextColor(getResources().getColor(R.color.black)); 
                                        commentBox.addView(commentView, commentViewParams);
                            }



                            }//end if key is == 1
                        else{
                            // Error in registration
                            registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
                        }//end else
                    }//end if
                } //end try

                catch (JSONException e) { 
                    e.printStackTrace();
                }//end catch    
            }
        }

【问题讨论】:

    标签: arrays json for-loop android-asynctask


    【解决方案1】:

    doInBackGround:方法被用作线程! onPostExecute:充当 UI 线程!

    所以尝试将你的任何长时间运行的代码放入 doInBackGround 方法中!

    当一个异步任务被执行时,任务会经过4个步骤:

    来自文档

    onPreExecute(),在任务执行之前在 UI 线程上调用。此步骤通常用于设置任务,例如通过在用户界面中显示进度条。

    doInBackground(Params...),在 onPreExecute() 执行完成后立即在后台线程上调用。此步骤用于执行可能需要很长时间的后台计算。异步任务的参数传递到这一步。计算的结果必须由这一步返回,并将传递回最后一步。此步骤还可以使用 publishProgress(Progress...) 来发布一个或多个进度单位。这些值在 UI 线程的 onProgressUpdate(Progress...) 步骤中发布。

    onProgressUpdate(Progress...),在调用 publishProgress(Progress...) 后在 UI 线程上调用。执行的时间是不确定的。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于动画进度条或在文本字段中显示日志。

    onPostExecute(Result),在后台计算完成后在 UI 线程上调用。后台计算的结果作为参数传递给这一步。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2020-08-15
      • 2014-03-29
      • 1970-01-01
      • 2020-08-19
      • 2017-06-18
      相关资源
      最近更新 更多