我一直在尝试编写一个允许用户从互联网上提取图像的 android。发布此应用程序后,有人告诉我,我需要 AsyncTask 以允许我使用网络命令。我一直在研究 Android 开发者网站 (developer.android.com/reference/android/os/AsyncTask.html) 上的 AsycTask 页面。这让我完全迷失了。我不知道我需要把这段代码放在哪里。这是我的原始代码,目前正在使用,直到发布。我怎样才能在这里实现 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;
}
特瓦丁顿的 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