异步任务返回什么

发布于 2025-01-06 05:29:30 字数 1103 浏览 0 评论 0原文

我有两个在APP启动时访问互联网的功能。我尝试使用 这篇 帖子作为参考,以便在我的内容加载。

我将使用的两个函数是:

getImage(); //Gets an image from the internet for an imageview
getJson();  //Where the app goes an parses a JSON object for a lazy load listview.

我在上面引用的帖子中遇到的问题是,我尝试使任务返回 null,但当我这样做时,它会导致应用程序崩溃。所以我有这个:

private class DownloadTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... args) {
    Log.i("MyApp", "Background thread starting");

    try {
        ImageView i = (ImageView) findViewById(R.id.currdoodlepic);
        Bitmap bitmap = BitmapFactory
                .decodeStream((InputStream) new URL(imageURL)
                        .getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    getJson("all");

    return "replace this with your data object";
}  

我不知道要返回什么。

I have two functions that access the internet on the APP start. I've tried to use this post as a reference in order to have the popup dialog while my content loads.

The two functions I would use are:

getImage(); //Gets an image from the internet for an imageview
getJson();  //Where the app goes an parses a JSON object for a lazy load listview.

The problem I'm encountering with the post I referenced above is that I try to make the task return null but it causes the app to crash when I do this. So I have this:

private class DownloadTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... args) {
    Log.i("MyApp", "Background thread starting");

    try {
        ImageView i = (ImageView) findViewById(R.id.currdoodlepic);
        Bitmap bitmap = BitmapFactory
                .decodeStream((InputStream) new URL(imageURL)
                        .getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    getJson("all");

    return "replace this with your data object";
}  

I'm not sure what to return.

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

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

发布评论

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

评论(2

み零 2025-01-13 05:29:30

doInBackground 方法的返回类型取决于执行后的需要:

void postExecute(Object result); // AsyncTask method

参数“result”是 doInBackground 的返回值。因此,如果您不需要任何东西,则返回 NULL。

The type return of the method doInBackground depend of what you need at the post execution :

void postExecute(Object result); // AsyncTask method

Parameter "result" is the return value of doInBackground. So if you need nothing you return NULL.

橘和柠 2025-01-13 05:29:30

我在此处找到了确切的答案。这是代码:

ImageView mChart = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...anything ...";

mChart.setTag(URL);
new DownloadImageTask.execute(mChart);

任务类:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
    this.imageView = imageViews[0];
    return download_Image((String)imageView.getTag());
}

@Override
protected void onPostExecute(Bitmap result) {
    imageView.setImageBitmap(result);
}


private Bitmap download_Image(String url) {
   ...
}

I found the exact answer here. Here is the code:

ImageView mChart = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...anything ...";

mChart.setTag(URL);
new DownloadImageTask.execute(mChart);

The Task class:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
    this.imageView = imageViews[0];
    return download_Image((String)imageView.getTag());
}

@Override
protected void onPostExecute(Bitmap result) {
    imageView.setImageBitmap(result);
}


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