异步任务返回什么
我有两个在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
doInBackground 方法的返回类型取决于执行后的需要:
参数“result”是 doInBackground 的返回值。因此,如果您不需要任何东西,则返回 NULL。
The type return of the method doInBackground depend of what you need at the post execution :
Parameter "result" is the return value of doInBackground. So if you need nothing you return NULL.
我在此处找到了确切的答案。这是代码:
任务类:
I found the exact answer here. Here is the code:
The Task class: