public class GetFlyers extends AsyncTask<String, Void, Bitmap> { ImageView imageView; int imageSize; Context context; String url=""; String im=""; Flyer fly; ImageFragment i1; int position; public GetFlyers(ImageView imageView, int imageSize, Context conext,String im, Flyer flu, ImageFragment i1, int position ) { this.im = im; this.position = position; this.imageView = imageView; this.imageSize = imageSize; this.context=context; this.i1 = i1; fly = flu; } protected Bitmap doInBackground(String... urls) { Bitmap map = null; for (String url : urls) { Log.d("firste","image loaded back"); if(url != null && !url.trim().equals("")) { map = downloadImage(url); this.url = url; } } return map; } @Override protected void onPostExecute(Bitmap result) { } private Bitmap downloadImage(String url) { Bitmap bitmap = null; InputStream stream = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inSampleSize = 1; try { stream = getHttpConnection(url); bitmap = BitmapFactory. decodeStream(stream, null, bmOptions); } catch (IOException e1) { e1.printStackTrace(); } finally { try { stream.close(); } catch(Exception e) {
}
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
我有这个代码,每当我调用这个帖子时......让我说3次.. GetFlyers task = new GetFlyers tasl.execute ...遗憾的是,每次拨打一个电话。只有一个asynctask去..我想在运行这个
时运行另一个asynctask答案 0 :(得分:3)
AsyncTasks默认从Honeycomb开始按顺序执行。
要在所有platrorms上并行运行AsyncTasks,请以
运行if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
task.execute();
}
见