我一直在尝试编程一个允许用户从互联网上取下图像的机器人。在发布这个应用程序之后,有人告诉我,我需要AsyncTask来允许我使用网络命令。我一直在研究Android开发者网站上的AsycTask页面(developer.android.com/reference/android/os/AsyncTask.html)。这让我彻底迷失了。我不知道我需要在哪里放置此代码。这是我原来的代码,目前正在发布。我如何在这里实现AsyncTask?
代码:
public void firstbutton(View view)
{
InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText editText = (EditText)findViewById(R.id.editText1);
inputMgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
EditText idnumber=(EditText)findViewById(R.id.editText1);
String idnumber2= idnumber.getText().toString();
int i = Integer.parseInt(editText.getText().toString());
idnum=i;
setContentView(R.layout.viewer);
Context context = view.getContext();
Drawable image = ImageOperations(context, "HIDDEN FOR PRIVACY"+idnumber2);
ImageView icon = new ImageView(context);
icon = (ImageView)findViewById(R.id.imageView1);
icon.setImageDrawable(image);
};
public Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException
{
URL url = new URL(address);
Object content = url.getContent();
return content;
}
TWADDINGTON的LOGCAT
11-21 20:02:30.987: E/AndroidRuntime(343): java.lang.RuntimeException: An error occured while executing doInBackground()
11-21 20:02:30.987: E/AndroidRuntime(343): at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-21 20:02:30.987: E/AndroidRuntime(343): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-21 20:02:30.987: E/AndroidRuntime(343): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-21 20:02:30.987: E/AndroidRuntime(343): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-21 20:02:30.987: E/AndroidRuntime(343): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-21 20:02:30.987: E/AndroidRuntime(343): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-21 20:02:30.987: E/AndroidRuntime(343): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-21 20:02:30.987: E/AndroidRuntime(343): at java.lang.Thread.run(Thread.java:1096)
11-21 20:02:30.987: E/AndroidRuntime(343): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
11-21 20:02:30.987: E/AndroidRuntime(343): at com.phil.avatarview.AvatarViewActivity$FetchImageAsyncTask.doInBackground(AvatarViewActivity.java:1)
11-21 20:02:30.987: E/AndroidRuntime(343): at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-21 20:02:30.987: E/AndroidRuntime(343): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-21 20:02:30.987: E/AndroidRuntime(343): ... 4 more
11-21 20:02:31.330: I/Process(61): Sending signal. PID: 343 SIG: 3
答案 0 :(得分:0)
像这样的东西,但我仍然建议(我为每个人都这样做)看起来只使用Loaders。例如AsyncTaskLoader
new AsyncTask<String, Void, Drawable>() {
@Override
protected Drawable doInBackground(String... params) {
return ImageOperations(getApplicationContext(), params[0]);
}
@Override
protected void onPostExecute(Drawable result) {
super.onPostExecute(result);
ImageView icon = new ImageView(context);
icon = (ImageView) findViewById(R.id.imageView1);
icon.setImageDrawable(image);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "HIDDEN FOR PRIVACY" + idnumber2);
答案 1 :(得分:0)
这样的事情应该让你开始:
/** A custom AsyncTask for downloading images in the background. */
private static class FetchImageAsyncTask extends AsyncTask<Void, Void, Drawable> {
private final String mUrl;
private final ImageView mImageView;
public FetchImageAsyncTask(ImageView view, String url) {
mUrl = url;
mImageView = view;
}
@Override
protected Drawable doInBackground(Void... params) {
// This aptly named method will run on a background Thread. This
// means your user interface will not be interrupted while the
// image is downloaded.
try {
URL url = new URL(mUrl);
// Instead of URL#getContent() you might consider using the
// DefaultHttpClient class.
return Drawable.createFromStream(
(InputStream) url.getContent(), "src");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Drawable result) {
// The result of doInBackground will be passed to this method, which
// is run on the UI Thread. This is important because you cannot
// update an ImageView with a new image from a background Thread.
if (result != null) {
mImageView.setImageDrawable(result);
}
}
}
public void firstbutton(View view) {
InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText editText = (EditText)findViewById(R.id.editText1);
inputMgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
EditText idnumber=(EditText)findViewById(R.id.editText1);
String idnumber2= idnumber.getText().toString();
int i = Integer.parseInt(editText.getText().toString());
idnum=i;
setContentView(R.layout.viewer);
Context context = view.getContext();
// Get your image view
ImageView iconView = (ImageView) findViewById(R.id.imageView1);
// Create a new background task
FetchImageAsyncTask task = new FetchImageAsyncTask(iconView, "YOUR_URL_HERE");
// Start the background task
task.execute();
}