使用 AsyncTask 检索多个字符串

Retrieving more than one string with an AsyncTask

我正在结合使用 AsyncTask 和 StreamScraper 来获取我正在开发的应用程序的 shoucast 元数据。现在,我只得到歌曲标题,但我也想得到流标题(这是通过 stream.getTitle(); 实现的)下面是我的 AsyncTask。

public class HarvesterAsync extends AsyncTask <String, Void, String> {

@Override
protected String doInBackground(String... params) {
    String songTitle = null;
    Scraper scraper = new ShoutCastScraper();
    List<Stream> streams = null;
    try {
        streams = scraper.scrape(new URI(params[0]));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ScrapeException e) {
        e.printStackTrace();
    }
    for (Stream stream: streams) {
        songTitle = stream.getCurrentSong();
    }
    return songTitle;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    MainActivity.songTitle.setText(s);
}
}

我需要更改什么才能获得多个字符串?

在这种情况下,从后台任务 return 多个值的最简单方法是 return 一个数组。

@Override
protected String[] doInBackground(String... params) {
    String songTitle = null;
    String streamTitle = null; // new
    Scraper scraper = new ShoutCastScraper();
    List<Stream> streams = null;
    try {
        streams = scraper.scrape(new URI(params[0]));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ScrapeException e) {
        e.printStackTrace();
    }
    for (Stream stream: streams) {
        songTitle = stream.getCurrentSong();
        streamTitle = stream.getTitle(); // new. I don't know what method you call to get the stream title - this is an example.
    }
    return new String[] {songTitle, streamTitle}; // new
}

@Override
protected void onPostExecute(String[] s) {
    super.onPostExecute(s); // this like is unnecessary, BTW
    MainActivity.songTitle.setText(s[0]);
    MainActivity.streamTitle.setText(s[1]); // new. Or whatever you want to do with the stream title.
}