简体   繁体   中英

Thread doesn't start until application restarted on Android

I am trying to get html data from a website and for now am simply changing a textview to the title of the webpage. For some reason this textview only displays the title whenever I exit the application and reopen it. I am guessing this is due to the threading and something that I do not know about it. As an example, here is my code for the stackoverflow.com website:

public void onResume() {
    super.onResume();

    Thread downloadThread = new Thread() {
        public void run() {
            Document doc;
            try {
                doc = Jsoup.connect("http://www.stackoverflow.com").get();
                titlestring = doc.title();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    downloadThread.start();

    TextView maintext = (TextView) findViewById(R.id.maintext);

    Log.i("wbbug", "Title string: " + titlestring);
    maintext.setText(titlestring);

}

For some reason, it does not show the title of the webpage until I start the application, press home, then reopen it. Why is this?

You are loading the webpage in a background thread(which is good), without waiting for the HTTP request to complete, you take titlestring and assume it contains the title of the webpage, but that will always not be the case.

Try this:

TextView maintext = (TextView)findViewById(R.id.maintext);
Thread downloadThread = new Thread() {                         
    public void run() {                                        
        Document doc;                                          
        try {                                              
            final Document doc = Jsoup.connect("http://www.stackoverflow.com").get();
            maintext.post(new Runnable(){
                maintext.setText(doc.title());
            });
        } catch (IOException e) {                       
            e.printStackTrace();                        
        }                                                       
    }                                                   
};                                                      
downloadThread.start();

Set text for the textview after you get the Document. Note setText() operates on UI component, so you have to do maintext.setText() in the UI thread, View.post() is one of a few ways of running code in UI thread.

Try this:

TextView maintext = (TextView) findViewById(R.id.maintext);
 Thread downloadThread = new Thread() {
        public void run() {
            Document doc;
            try {
                doc = Jsoup.connect("http://www.stackoverflow.com").get();
                titlestring = doc.title();

             runOnUiThread(new Runnable()
                     {
                       run(){
                    Log.i("wbbug", "Title string: " + titlestring);
                    maintext.setText(titlestring);
                            }
                     }

                } catch (IOException e) {
                e.printStackTrace();
                }
        }
    };
    downloadThread.start();

Your maintext.setText() invokes before you get the title of your page but at the second time the titlestring is already initiated with your value and setText get your page name.

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