code是postExecute工作laggy,但不会在doInBackground工作? [英] code is working laggy in postExecute but won't work in doInBackground?

查看:226
本文介绍了code是postExecute工作laggy,但不会在doInBackground工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获取专辑封面,在一个的AsyncTask类last.fm的API。在code工作在postExecute()与延迟,但它不会在doInBackground()在所有的工作..它是不是我的UI进行更新可言,所以我不知道发生了什么事。

  @覆盖
公共无效doInBackground(字符串参数... args){    如果(oslist.size()!= 0){
        对(INT I = 0; I&小于30;我++){            字符串albumURL = NULL;            尝试{
                 albumURL =新RetrieveAlbumArtUrlTask​​()执行(的String.format(APIURL,
                         URLEn coder.en code(oslist.get(I)获得(TAG_ARTIST),UTF-8),
                         URLEn coder.en code(oslist.get(I)获得(TAG_ALBUM),UTF-8)))得到()。
            }赶上(UnsupportedEncodingException五){            }赶上(InterruptedException的E){            }赶上(为ExecutionException E){            }            oslist.get(ⅰ)。把(albumArtUrl,albumURL);
            Log.v(TEST,+ albumURL);        }
    }    返回null;
}


解决方案

这是的AsyncTask 不以这种方式泰迪工作。在的AsyncTask 被分解成两部分。在 doInBackground()段是为了处理非UI组件,因为它的工作原理UI线程之外。当你需要从内部进行更改UI中的的AsyncTask ,你叫 publishProgress()使用自定义标识符区分你想更新的东西。

我经常这样做有一个枚举。然后你会看到你的用户界面的变化发生,从落后的用户界面中的非UI组件。刚上的AsyncTask 例子读了一下。

I am fetching album art with the last.fm api in an asynctask class. The code works in postExecute() with latency, though it won't work at all in doInBackground().. it is not making updates to my UI at all, so i'm not sure what's going on

@Override
public Void doInBackground(String... args){

    if(oslist.size()!=0) {
        for (int i = 0; i < 30; i++) {

            String albumURL = null;

            try{
                 albumURL = new RetrieveAlbumArtUrlTask().execute(String.format(APIURL,
                         URLEncoder.encode(oslist.get(i).get(TAG_ARTIST), "UTF-8"),
                         URLEncoder.encode(oslist.get(i).get(TAG_ALBUM), "UTF-8"))).get();


            } catch (UnsupportedEncodingException e) {

            } catch (InterruptedException e) {

            } catch(ExecutionException e){

            }

            oslist.get(i).put("albumArtUrl", albumURL);
            Log.v("TEST", ""+albumURL);

        }
    }

    return null;
}

解决方案

An AsyncTask does not work in that manner Teddy. The AsyncTask is broken up into two parts. The doInBackground() segment is meant to handle non-UI components because it works outside the UI thread. When you need to make a change to UI from within the AsyncTask, you call publishProgress() with a custom identifier to distinguish what you want updated.

I often do this with an enum. Then you shall see your UI changes occurring and your non-UI components from lagging the UI. Just read up on the AsyncTask examples a bit.

这篇关于code是postExecute工作laggy,但不会在doInBackground工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆