简体   繁体   中英

Using AsyncTask to update my UI (Android)

I have a long running code which parses an XML and updates some TextView on my UI, the code works well but I need to put it in an AsyncTask, I'm quite new to Android and I've attempted to do this but it didn't work. Can someone please tell me what I've done wrong.

class UpdaterThread extends AsyncTask<Void, String, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        try {
            URL url = new URL(
                    "http://www.google.com/ig/api?weather=Cardiff");

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            XMLReader xr = sp.getXMLReader();
            WeatherHandler myWeatherHandler = new WeatherHandler();
            xr.setContentHandler(myWeatherHandler);

            xr.parse(new InputSource(url.openStream()));

        }

        catch (Exception e) {

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        SetWeather mySetWeather = new SetWeather();
        temp.setText(mySetWeather.getTemp());
        wind.setText(mySetWeather.getWind());
        humidity.setText(mySetWeather.getHumidity());
    }

}

I've also use the.execute() method in my main class to run it.

Is it possible to simultaneously update two UI while getting the progress smoothly? SOmetimes it renders extremely bad performamnce in terms of download speed and UI notification handler not responsive unless adding a thread.sleep();

Two for the price of one: the AsyncTask question at android AsyncTask xml parsing gives you some context for XML parsing using an AsyncTask.

First:

SetWeather mySetWeather = new SetWeather();
        temp.setText(mySetWeather.getTemp());

Where do you add the parsed information to the new object created above?

What is temp? Where is it defined?

Second, personally i prefer a callback to the activity sending the new object, and then in the activity change the UI, but this is a personal preference.

There are two things I immediately noticed that could be wrong or not what you intended:

  1. Normally you'd want to return the value obtained from doInBackground() for use in onPostExecute() , but you are returning null, which is then being passed in as the result. Is this what you want?
  2. Based on that question, you'll need to change the AsyncTask types to something more fitting. Since you're simply returning null , setting the second type to String works for now, but if you return a new value, you'll need to update this type. The second type should probably be Void anyway, since that is the convention for unused types (since you don't implement onProgressUpdate() ).

More background on what you're hoping to accomplish along with sample code would be helpful to figuring out the problem as well.

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