AsyncTask类解析XML

发布于 2024-12-04 05:27:40 字数 2028 浏览 0 评论 0原文

当我将这段代码放在 UI 线程中但想将其移动到 AsyncTask 类中时,该代码可以工作。我刚刚读完如何执行此操作,但是当我运行该应用程序时,它在开始阅读课程时崩溃了。

该类在将 XML 文件加载到列表之前调用并解析它。这是我的第一个 Android 应用程序,因此您可以提供的任何帮助都是适用的。

谢谢

public class DownloadXml extends AsyncTask<String, Void, String>
{       
    private ProgressDialog pd;
    protected String doInBackground(String... platform) 
    {
        pd = ProgressDialog.show(CategoryData.this, "Please Wait...", "Loading games");

        try
        {
            BaseFeedParser parser = new BaseFeedParser();
            messages = parser.parse();
        }
        catch (Throwable t)
        {
            Log.e("RetailApp",t.getMessage(),t);
        }
        return rowPlatform;
    }


    protected void onPostExecute(String rowPlatform)
    {
        productList = new ArrayList<HashMap<String, Object>>();

            for (Message msg : messages)
            {   
                if(msg.getPlatform().compareTo(rowPlatform) == 0 )
                {   
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("Title", msg.getTitle());
                    map.put("Brand", msg.getBrand());
                    map.put("Price", "$" + msg.getPrice());

                    try
                    {
                        String picture[] = msg.getImage().split("-large.jpg");
                        URL pictureURL = new URL(picture[0] + "-medium.jpg");
                        Bitmap bitmap = BitmapFactory.decodeStream(pictureURL.openStream());
                        map.put("Img",bitmap);
                    }
                catch (Throwable t)
                {
                    Log.e("RetailApp",t.getMessage(),t);
                }
                productList.add(map);
                } 
            }

编辑:我已将“for”循环移至 onPostExecute 中,这似乎改进了代码,但我仍然收到“未注册的句柄仍处于锁定状态”错误。有什么想法吗?

感谢 Rob 的想法,但运气不好,已从后台删除了循环和 Toast...上面的新代码。我收到错误“未捕获的处理程序:线程 AsycTask #1 由于未捕获的异常而退出”

This code worked when I had it in the UI thread but wanted to move it into a AsyncTask class. I have just finished reading up how to do this but when I run the app it is now crashing when it starts reading the class.

The class calls and parses an XML files before loading it into a List. This is my first Android app, so any help you can provide is appricated.

Thanks

public class DownloadXml extends AsyncTask<String, Void, String>
{       
    private ProgressDialog pd;
    protected String doInBackground(String... platform) 
    {
        pd = ProgressDialog.show(CategoryData.this, "Please Wait...", "Loading games");

        try
        {
            BaseFeedParser parser = new BaseFeedParser();
            messages = parser.parse();
        }
        catch (Throwable t)
        {
            Log.e("RetailApp",t.getMessage(),t);
        }
        return rowPlatform;
    }


    protected void onPostExecute(String rowPlatform)
    {
        productList = new ArrayList<HashMap<String, Object>>();

            for (Message msg : messages)
            {   
                if(msg.getPlatform().compareTo(rowPlatform) == 0 )
                {   
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("Title", msg.getTitle());
                    map.put("Brand", msg.getBrand());
                    map.put("Price", "$" + msg.getPrice());

                    try
                    {
                        String picture[] = msg.getImage().split("-large.jpg");
                        URL pictureURL = new URL(picture[0] + "-medium.jpg");
                        Bitmap bitmap = BitmapFactory.decodeStream(pictureURL.openStream());
                        map.put("Img",bitmap);
                    }
                catch (Throwable t)
                {
                    Log.e("RetailApp",t.getMessage(),t);
                }
                productList.add(map);
                } 
            }

Edit: I have moved the 'for' loop into onPostExecute and that seems to improve the code but I am still receiving a 'unregistered handle still locked' error. Any ideas?

Thanks for the idea Rob but no luck, have removed loop and Toast from background... new code above. Am receiving the error "Uncaught handler: thread AsycTask #1 exiting due to uncaught exception"

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

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

发布评论

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

评论(2

紫轩蝶泪 2024-12-11 05:27:40

使用您的 LogCat。 AndroidRuntime 日志将为您提供完整的堆栈跟踪和强类型异常,以准确显示崩溃的原因。

我很确定您正在尝试从后台线程显示 ProgressDialog 。您应该在执行 AsyncTask 之前在 UI 线程上执行此操作。您可以在 onPostExecute 中关闭它,因为它会在 UI 线程上运行

Use your LogCat. The AndroidRuntime log will give you a full stack trace and a strongly typed exception that will show you exactly why it crashed.

I'm pretty sure it's the fact that you're trying to show a ProgressDialog from the background thread. You should do that on the UI thread just before you execute your AsyncTask. You can dismiss it in onPostExecute, since that runs back on the UI thread

勿忘初心 2024-12-11 05:27:40

pd = ProgressDialog.show(CategoryData.this, "Please Wait...", "Loading games");

发生在 onPreExecute() 中,它在开始执行之前在 UI 线程上调用。

this

pd = ProgressDialog.show(CategoryData.this, "Please Wait...", "Loading games");

goes in onPreExecute(), which is called on the UI Thread before execute is started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文