Android AsyncTask 帮助

发布于 2024-11-06 22:50:03 字数 1450 浏览 0 评论 0原文

我的应用程序在所有版本的 Android 中都工作正常,除了它失败,因为我在主线程上进行网络调用。我下面的代码在 url.openStream() 上失败,我想我需要在单独的线程上进行此调用。另外,我创建了此类Utilities,以便我可以引用我在应用程序的许多部分中使用的函数。我确信我做错了,但这就是我当时所知道的。我希望有人愿意花时间向我展示应该如何完成此操作,以及如何将 openStream 调用包装在 AsyncTask 中。预先感谢您的帮助。另外,过去几天我一直在尝试自己解决这个问题。

class Utilities {
    static XMLReader xr;
    static URL url;
    private final static int CHUNK_SIZE = 32 * 1024;
    static byte[] _fileIOBuffer = new byte[CHUNK_SIZE];

static String DBGetOnlineVersionNumber(final Activity activity){

    final String version = null;

    try {

        /* Create a URL we want to load some xml-data from. */
        String location = activity.getString(R.string.version_file_location);
        url = new URL(location);

        /* Get a SAXParser from the SAXPArserFactory. */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();

        /* Get the XMLReader of the SAXParser we created. */
        xr = sp.getXMLReader();
        /* Create a new ContentHandler and apply it to the XML-Reader*/
        final XMLVersionReader myExampleHandler = new XMLVersionReader();
        xr.setContentHandler(myExampleHandler);

        xr.parse(new InputSource(url.openStream())); //Fails here
        /* Parsing has finished. */
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }
    return version;
}

My application works fine in all versions of Android except It is failing because I am making network calls on the main thread. My code below fails on url.openStream() and I guess I need to make this call on a separate thread. Also, I created this class Utilities so I could reference functions that I use in many parts of my application. I am sure I went about this wrong, but it was all I knew at the time. I am hoping someone is willing to take the time to show me how I should have done this and how I can wrap the openStream call in an AsyncTask. Thanks in advance for your help. Also, I have spent the last several days trying to figure it out on my own.

class Utilities {
    static XMLReader xr;
    static URL url;
    private final static int CHUNK_SIZE = 32 * 1024;
    static byte[] _fileIOBuffer = new byte[CHUNK_SIZE];

static String DBGetOnlineVersionNumber(final Activity activity){

    final String version = null;

    try {

        /* Create a URL we want to load some xml-data from. */
        String location = activity.getString(R.string.version_file_location);
        url = new URL(location);

        /* Get a SAXParser from the SAXPArserFactory. */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();

        /* Get the XMLReader of the SAXParser we created. */
        xr = sp.getXMLReader();
        /* Create a new ContentHandler and apply it to the XML-Reader*/
        final XMLVersionReader myExampleHandler = new XMLVersionReader();
        xr.setContentHandler(myExampleHandler);

        xr.parse(new InputSource(url.openStream())); //Fails here
        /* Parsing has finished. */
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }
    return version;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2024-11-13 22:50:03

现在您已经很好地将代码放入单独的静态方法中,我猜使用它更合适。让您可以同步调用它,或者可以从单独的线程调用相同的代码。如果将调用添加到您的 Activity 中,则可以将调用包装到 AsyncTask 中。

new AsyncTask<Activity, Void, String>() {
    @Override
    public String doInBackground(Activity... activities) {
        return Utilities.DBGetOnlineVersionNumber(activities[0]);
    }
    @Override
    public void onPostExecute(String result) {
        Log.d("VERSION_NUMBER", result);
    }
}.execute(this);

Now that you have nicely put your code in a separate static method I'm guessing it's more appropriate to use it instead. Leaving you with possibility to call it synchronously or same code can be called from separate Thread. Wrapping the call into AsyncTask can be done somewhere around this if added to your Activity.

new AsyncTask<Activity, Void, String>() {
    @Override
    public String doInBackground(Activity... activities) {
        return Utilities.DBGetOnlineVersionNumber(activities[0]);
    }
    @Override
    public void onPostExecute(String result) {
        Log.d("VERSION_NUMBER", result);
    }
}.execute(this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文