Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 代码在postExecute中运行缓慢,但赢得了';你不在后台工作吗?_Java_Android_Memory_Android Asynctask_Last.fm - Fatal编程技术网

Java 代码在postExecute中运行缓慢,但赢得了';你不在后台工作吗?

Java 代码在postExecute中运行缓慢,但赢得了';你不在后台工作吗?,java,android,memory,android-asynctask,last.fm,Java,Android,Memory,Android Asynctask,Last.fm,我正在asynctask类中使用last.fm api获取相册艺术。代码在具有延迟的postExecute()中工作,但在doInBackground()中根本不起作用。。它根本没有更新我的UI,所以我不知道发生了什么 @Override public Void doInBackground(String... args){ if(oslist.size()!=0) { for (int i = 0; i < 30; i++) { Str

我正在asynctask类中使用last.fm api获取相册艺术。代码在具有延迟的postExecute()中工作,但在doInBackground()中根本不起作用。。它根本没有更新我的UI,所以我不知道发生了什么

@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;
}
@覆盖
公共Void doInBackground(字符串…参数){
如果(oslist.size()!=0){
对于(int i=0;i<30;i++){
字符串URL=null;
试一试{
albumURL=new RetrieveAlbumArtUrlTask().execute(String.format(APIURL,
URLEncoder.encode(oslist.get(i).get(TAG_-ARTIST),“UTF-8”),
encode(oslist.get(i.get(TAG_相册),“UTF-8”)).get();
}捕获(不支持的编码异常e){
}捕捉(中断异常e){
}捕获(执行例外){
}
oslist.get(i).put(“albumArtUrl”,albumURL);
Log.v(“TEST”和“+albumURL”);
}
}
返回null;
}
您有两种选择: 1.将要更新的数据返回到onPostExecute并从那里更新UI。。不要在postExecute上执行长操作。。
2.如果您在Activity类上运行此功能,您可以在doInBackground中使用
RunOnUIThread
函数并从中更新UI。

异步任务不能以这种方式工作。
AsyncTask
分为两部分。
doInBackground()
段用于处理非UI组件,因为它在UI线程之外工作。当您需要从
AsyncTask
中更改UI时,可以使用自定义标识符调用
publishProgress()
,以区分要更新的内容


我经常使用枚举来执行此操作。然后,您将看到您的UI发生了变化,并且您的非UI组件落后于UI。只需稍微阅读一下
AsyncTask
示例。

您需要在主应用程序线程上执行
AsyncTask
RetrieveAlbumArtUrlTask
似乎是一个
AsyncTask
。除此之外,请记录您的异常,然后使用Traceview确定延迟的来源。