2016-10-22 30 views
1

我想问一下,这对我来说看起来像一个问题是问题。Android退出AsyncTask问题

我有一类AsyncTask从一个json文件和一个doInBackground方法获取数据与预执行和后执行方法。

在我的MainActivity的onCreate方法中,我使用name.execute()调用AsyncTask的类。问题在于程序陷入了post执行方法,这是一个问题吗?有一种方法可以返回到OnCreate方法,还是应该继续使用post执行方法中的代码?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    new LoadAllProducts().execute(); 
    txtView=(TextView) findViewById(R.id.txtV); 

} 

class LoadAllProducts extends AsyncTask<String, String, String> { 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Loading questions. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 


    /** 
    * getting All products from url 
    */ 
    protected String doInBackground(String... args) { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     // getting JSON string from URL 
     JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); 

     // Check your log cat for JSON reponse 
     Log.d("All questions: ", json.toString()); 

     try { 
      // Checking for SUCCESS TAG 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // products found 
       // Getting Array of Products 
       questions = json.getJSONArray(TAG_QUESTIONS); 

       // looping through All Products 
       for (int i = 0; i < questions.length(); i++) { 
        JSONObject c = questions.getJSONObject(i); 

        // Storing each json item in variable 
        int id = c.getInt(TAG_QID); 
        String questionPron = c.getString(TAG_QUESTION); 
        String answer1 = c.getString(TAG_ANSWER_1); 
        String answer2 = c.getString(TAG_ANSWER_2); 
        String answer3 = c.getString(TAG_ANSWER_3); 
        String answer4 = c.getString(TAG_ANSWER_4); 
        int level = c.getInt(TAG_LEVEL); 
        int correctIs = c.getInt(TAG_CORRECT_IS); 
        // String updatedAt = c.getString(TAG_UPDATED_AT); 

        dokimi = questionPron; 
        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        //ArrayList<eachQuestion> qArray = new ArrayList<eachQuestion>(); 

        eachQuestion ea = new eachQuestion(); 

        ea.setId(id); 
        ea.setQuestionPron(questionPron); 
        ea.setAnswer1(answer1); 
        ea.setAnswer2(answer2); 
        ea.setAnswer3(answer3); 
        ea.setAnswer4(answer4); 
        ea.setLevel(level); 
        ea.setCorrectIs(correctIs); 

        // adding each child node to HashMap key => value 
        //map.put(TAG_PID, id); 
        //map.put(TAG_NAME, name); 

        qArray.add(ea); 




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

     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all products 
     pDialog.dismiss(); 
     // updating UI from Background Thread 
     runOnUiThread(new Runnable() { 
      public void run() { 


      } 
     }); 

    } 



} 
+0

你是什么意思“程序卡在执行后的方法” –

+1

没有办法返回到'OnCreate()'。在我看来,你需要阅读更多关于基本Android编程和异步调用 – Merlevede

+0

'新的LoadAllProducts()。execute();'立即返回并且'onCreate'可能在任务开始执行之前完成。 'onPostExecute'中的代码将在'doInBackground'返回后执行(这可以随时发生)。 – trooper

回答

0

“问题是程序卡在post执行方法中,是一个问题吗?”我只能猜测这应该是什么意思,但我会尽力回答你的问题。 AsyncTask甚至存在的原因是它的代码在单独的线程(cpu)上运行。主线程(cpu)正在使的另一个 cpu执行给定的代码。

这就是为什么方法调用​​几乎立即返回,很可能甚至在另一个cpu的任何给定代码行执行之前。您无法预测该代码的执行时间(如果有的话)。这取决于您的操作系统的调度程序和当前的运行时会话,在计算机科学中,我们描述的行为如undeterministic

+0

好的,真的非常感谢你的解释! 但是,如果我想调用另一个活动,我应该将代码写入onPostExecute还是其他地方,那么继续该项目的最佳方式是什么? 对不起,如果我问“傻”的事情,但我想弄明白。 – billGmst

+0

是的,'onPostExecute()'是在后台处理后继续执行程序工作流的一种自然方式。请记住,这个方法在主线程上再次执行,所以你不应该在那里执行大量的计算(虽然启动另一个活动是完全OK的)。 –

+0

非常感谢!你非常有帮助! – billGmst