如何把JSON的里面的AsyncTask - 机器人机器人、里面、JSON、AsyncTask

2023-09-06 00:03:50 作者:戏

我有一个JSON功能连接到数据库并返回结果。它这样做是约15倍,或有多少意见有在数据库中。 JSON的功能是在while循环中,重复自己,直到所有的评论已经采取了从数据库或直至达到15条评论。问题是,当应用程序加载它做它的评论应用程序的OnCreate部分中。我希望应用程序加载,然后将JSON功能,在后面加载。我知道我可以用一个AsyncTask的做到这一点,但我不熟悉他们。所以,我希望有人能告诉我怎么把这个code到AsyncTask的。

I have a json function that connects to a database and returns a result. It does this about 15 times or for how many comments there are in the database. 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. The problem is when the app loads the comments it does it during the onCreate part of the app. I want the app to load and then the json function to load in the back. I know I can do this with an asynctask but I am not familiar with them. So I was hoping someone would be able to tell me how to place this code into a 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       

这一切,code ++工程,但我想它在后台运行不会在应用程序的OnCreate有人请尽量把这个变成一个AsyncTask的,或至少帮助我了解如何做到这一点。

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.

推荐答案

您应该把您的while循环在一个新的线程或异步任务。这是怎么将工作

You should put your while loop in a new Thread or Async Task. 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

    }


  }

就在你目前的code while循环之前,你应该叫新JsonWork()。执行()。因此,这将在一个新的AsyncTask线程执行while循环。

Just before the while loop in your current code you should call new JsonWork().execute(). So that it would execute the while loop in a new AsyncTask Thread.

 
精彩推荐
图片推荐