简体   繁体   中英

Android exit AsyncTask issue

I want to ask if this that looks like an issue to me is problem.

I have a class of AsyncTask to get data from a json file and a doInBackground method with pre-execute and post-execute methods.

At onCreate method of my MainActivity I call the class of AsyncTask with name.execute(). The problem is that the program stuck into the post execute method, is that a problem? There is a way to return to the OnCreate method or should I continue with my code from post execute method?

@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() {


            }
        });

    }



}

"The problem is that the program stuck into the post execute method, is that a problem?" I can only guess what that was supposed to mean but I will try to answer your question. The reason why AsyncTask even exists is that its code is run on a separate thread (cpu). The main thread (cpu) is making another cpu execute given code.

That is why method call of execute() returns almost immediately, most probably even before any line of given code for another cpu executes. You are not able to predict when exactly (if ever) this code will execute. This depends on your OS's scheduler and current runtime session, in computer science we describe such behaviour as undeterministic .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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