简体   繁体   English

如何将Json放入asynctask-Android

[英]How to put Json inside asynctask - Android

I have a json function that connects to a database and returns a result. 我有一个连接到数据库并返回结果的json函数。 It does this about 15 times or for how many comments there are in the database. 它执行此操作大约15次或数据库中有多少条注释。 The json function is inside a while loop, and repeats itself until all the comments have been taken from the database or until it reached 15 comments. json函数位于while循环内,并重复自身,直到从数据库中获取所有注释或到达15条注释为止。 The problem is when the app loads the comments it does it during the onCreate part of the app. 问题是,当应用程序加载注释时,它将在应用程序的onCreate部分执行该操作。 I want the app to load and then the json function to load in the back. 我希望应用程序加载,然后在后面加载json函数。 I know I can do this with an asynctask but I am not familiar with them. 我知道我可以使用asynctask来做到这一点,但我对它们并不熟悉。 So I was hoping someone would be able to tell me how to place this code into a asynctask. 因此,我希望有人能够告诉我如何将此代码放入asynctask中。

     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);



                    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);



                                 verify2 = verify2 + 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       

All this code works but I want it running in the background not during the apps oncreate some one please try putting this into a asynctask or at least help me understand how to do so. 所有这些代码都有效,但是我希望它不在后台运行,而不是在应用程序创建某个代码的过程中尝试将其放入asynctask或至少帮助我了解如何做到这一点。

You should put your while loop in a new Thread or Async Task. 您应该将while循环放入新的线程或异步任务中。 Here is how is will work 这是如何工作的

    public class JsonWork extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
    //your while loop goes here

    }


  }

Just before the while loop in your current code you should call new JsonWork().execute() . 在当前代码的while循环之前,您应该调用new JsonWork().execute() So that it would execute the while loop in a new AsyncTask Thread. 这样它将在新的AsyncTask线程中执行while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM