Android 如何在asynctask中放置json代码

Android 如何在asynctask中放置json代码,android,json,android-asynctask,Android,Json,Android Asynctask,我有一个连接到数据库并检索注释的json函数。该函数首先收集有多少条注释,然后while循环调用该函数,直到检索到所有注释。问题是,当我加载我的应用程序时,会出现黑屏,在Json函数完成之前不会加载其余视图。我希望json函数在后台运行。因此,应用程序将加载,评论将在后台单独加载。下面是收集所有注释的代码 String usernameforcomments = db.getUserDetails(); registerErrorMsg =

我有一个连接到数据库并检索注释的json函数。该函数首先收集有多少条注释,然后while循环调用该函数,直到检索到所有注释。问题是,当我加载我的应用程序时,会出现黑屏,在Json函数完成之前不会加载其余视图。我希望json函数在后台运行。因此,应用程序将加载,评论将在后台单独加载。下面是收集所有注释的代码

    String usernameforcomments = db.getUserDetails();



                     registerErrorMsg = (TextView) findViewById(R.id.collectComment_error);
                     int verify = 1;
                     int verify2 = 1;
                     String offsetNumber = "null";
                     LinearLayout linear = (LinearLayout) this.findViewById(R.id.commentSection);

                     UserFunctions CollectComments = new UserFunctions();
                     JSONObject json = CollectComments.collectComments(usernameforcomments, offsetNumber);




                         int commentCycle = 1;

                  // check for comments  
                     try {  
                         if (json.getString(KEY_SUCCESS) != null) { 
                             registerErrorMsg.setText("");
                             String res2 = json.getString(KEY_SUCCESS);
                             if(Integer.parseInt(res2) == 1){ 

                                 String numberOfComments = json.getString(KEY_NUMBER_OF_COMMENTS);


                                 String offsetNumberDb = db.getOffsetNumber();


                                int numberOfComments2 = Integer.parseInt(numberOfComments) - Integer.parseInt(offsetNumberDb);
                                offsetNumber = offsetNumberDb;

                                 //if comment number is less than 15 or equal to 15
                                 if(numberOfComments2 <= 15){




                                 while (commentCycle <= numberOfComments2){

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


                                    LinearLayout commentBox = new LinearLayout(this);
                                    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);

                                     RelativeLayout commentBoxHeader = new RelativeLayout(this);
                                    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);


                                    TextView usernameView = new TextView(this);
                                    usernameView.setText("Posted by: " + json2.getString(KEY_USERNAME));
                                    RelativeLayout.LayoutParams usernameViewParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                                    usernameViewParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                                     usernameView.setTextColor(getResources().getColor(R.color.white));
                                     usernameView.setPadding(5, 5, 10, 5);
                                     usernameView.setTypeface(Typeface.DEFAULT_BOLD);
                                    commentBoxHeader.addView(usernameView, usernameViewParam);


                                    TextView commentView = new TextView(this);
                                    commentView.setText(json2.getString(KEY_COMMENT));
                                    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);

                                    TextView descriptionView = new TextView(this);
                                    descriptionView.setText(json2.getString(KEY_COMMENT));
                                    descriptionView.setPadding(20, 10, 20, 20);
                                    descriptionView.setLayoutParams(new LinearLayout.LayoutParams(
                                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                                    descriptionView.setTextColor(getResources().getColor(R.color.black)); 
                                    commentBox.addView(descriptionView);

                                    RelativeLayout commentBoxButtons = new RelativeLayout(this);
                                    RelativeLayout.LayoutParams commentBoxButtonsParam = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                                     commentBox.addView(commentBoxButtons, commentBoxButtonsParam);

                                    Button btnTag1 = new Button(this);
                                    RelativeLayout.LayoutParams btnTag1Param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                                    btnTag1Param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                                     btnTag1.setText("Go to " + json2.getString(KEY_PLATENUMBER));
                                     btnTag1Param.setMargins(10, 10, 10, 10);
                                     btnTag1.setBackgroundResource(R.drawable.dashboard_post);
                                     btnTag1.setTextColor(getResources().getColor(R.color.white));
                                     btnTag1.setTypeface(Typeface.DEFAULT_BOLD);
                                     btnTag1.setPadding(8, 5, 8, 5);
                                     btnTag1.setId(verify);
                                     commentBoxButtons.addView(btnTag1, btnTag1Param);

                                     Button btnTag2 = new Button(this);
                                     RelativeLayout.LayoutParams btnTag2Param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                                    btnTag2Param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                                     btnTag2.setText("Go to " + json2.getString(KEY_USERNAME));
                                     btnTag2Param.setMargins(10, 10, 10, 10);
                                     btnTag2.setTypeface(Typeface.DEFAULT_BOLD);
                                     btnTag2.setBackgroundResource(R.drawable.dashboard_post);
                                     btnTag2.setTextColor(getResources().getColor(R.color.white));
                                     btnTag2.setPadding(8, 5, 8, 5);
                                     btnTag2.setId(verify2); 
                                     commentBoxButtons.addView(btnTag2, btnTag2Param);

                                    verify2 = verify2 + 1;
                                    verify = verify + 1; 
                                    offsetNumber = json2.getString(KEY_OFFSET_NUMBER);
                                    commentCycle = commentCycle + 1;

                             }//end while
                                 }//end if comment number is less than or equal to 15



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

                     catch (JSONException e) { 
                         e.printStackTrace();
                     }//end catch   
String usernameforcomments=db.getUserDetails();
registerErrorMsg=(TextView)findViewById(R.id.collectComment\u错误);
int=1;
int-verify2=1;
字符串offsetNumber=“null”;
LinearLayout linear=(LinearLayout)this.findViewById(R.id.commentSection);
UserFunctions CollectComments=新的UserFunctions();
JSONObject json=CollectComments.CollectComments(usernameforcomments,offsetNumber);
int循环=1;
//查看评论
试试{
if(json.getString(KEY_SUCCESS)!=null){
registerErrorMsg.setText(“”);
String res2=json.getString(KEY_SUCCESS);
如果(Integer.parseInt(res2)==1){
String numberOfComments=json.getString(注释的键号);
字符串offsetNumberDb=db.getOffsetNumber();
int numberOfComments2=Integer.parseInt(numberOfComments)-Integer.parseInt(offsetNumberDb);
offsetNumber=offsetNumberDb;
//如果注释编号小于15或等于15

如果(numberOfComments2我不会为您重新编写代码,但您可以尝试一下,然后在遇到问题的地方发布。您可以使用
doInBackground()
以外的任何方法更新
UI
元素。因此,请设置
AyncTask
()如果您需要在任务开始之前使用
UI
执行某些操作,例如显示
ProgressBar
,则可以将其放入
onPreExecute()

将所有JSON转换放入
doInBackground()
中,如果在获取数据时需要更新
UI
,则可以使用
publishProgress()
from
doInBackground()
调用
onProgressUpdate()

如果您需要
doInBackground()
返回
数据,那么它将
数据作为参数返回到
onPostExecute()
,然后您可以再次更新
UI
,调用
活动
方法等