如何将 Json 放入 asynctask - Android [英] How to put Json inside asynctask - Android

查看:38
本文介绍了如何将 Json 放入 asynctask - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到数据库并返回结果的 json 函数.它这样做了大约 15 次或数据库中有多少评论.json 函数在一个 while 循环中,并不断重复,直到从数据库中取出所有评论或直到达到 15 条评论.问题是当应用加载评论时,它是在应用的 onCreate 部分执行的.我希望应用程序加载,然后 json 函数在后面加载.我知道我可以用 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       

所有这些代码都有效,但我希望它在后台运行,而不是在创建应用程序期间运行,请尝试将其放入异步任务中,或者至少帮助我了解如何执行此操作.

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

    }


  }

就在当前代码中的 while 循环之前,您应该调用 new JsonWork().execute().这样它就会在新的 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.

这篇关于如何将 Json 放入 asynctask - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆