运行 asynctask 里面的asynctask 怎么在同一时间?
how run asynctask inside asynctask in the same time?
我有一个基于 Web 的应用程序,它必须在检索到一个文本后检索外部数据库中的文本和服务器中的图像,使用以下代码检索文本:
class BackgroundTaskOne extends AsyncTask<Void, Void, String>
{
String json_url;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
json_url="http://..some url../somefile.php";
}
@Override
protected String doInBackground(Void... voids) {
// TODO Auto-generated method stub
String error="";
try {
URL url=new URL(json_url);
HttpURLConnection httpUrlConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=httpUrlConnection.getInputStream();
BufferedReader bufferReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
while((JSON_STRINO=bufferReader.readLine())!=null){
stringBuilder.append(JSON_STRINO+"\n");
//add new textView
}
bufferReader.close();
inputStream.close();
httpUrlConnection.disconnect();
json_string=stringBuilder.toString().trim();
return stringBuilder.toString().trim();
// return "one row of data inserted..";
} catch (MalformedURLException e) {
error=e.getMessage()+" first";
} catch (IOException e) {
// TODO Auto-generated catch block
error=e.getMessage()+" sec";
}
return error;
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
json_string=result;
l=parse();
int i=0;
for(final String a[]:l){
TextView t=new TextView(MainActivity.this);
final String path=a[2]+".jpg";
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t.setText(a[0]+" : "+a[1]+" i="+i);
t.setId(i);
Reklam.addView(t);
new LoadImage().execute(path);
i++;
}}}
我想在依次加载第一个文本和第二个文本之后加载图像时加载图像,但它先加载所有文本,然后再加载所有图像,这些用于加载图像的代码如下:
private class LoadImage extends AsyncTask<String, String, Bitmap> {
Bitmap bitmap;
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
ImageView img=new ImageView(MainActivity.this);
img.setImageBitmap(image);
img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Reklam.addView(img);
pDialog.dismiss();
}else{
pDialog.dismiss();
Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
如果你想运行多线程并行:
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
new MyAsyncTask().execute();
}
我有一个基于 Web 的应用程序,它必须在检索到一个文本后检索外部数据库中的文本和服务器中的图像,使用以下代码检索文本:
class BackgroundTaskOne extends AsyncTask<Void, Void, String>
{
String json_url;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
json_url="http://..some url../somefile.php";
}
@Override
protected String doInBackground(Void... voids) {
// TODO Auto-generated method stub
String error="";
try {
URL url=new URL(json_url);
HttpURLConnection httpUrlConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=httpUrlConnection.getInputStream();
BufferedReader bufferReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
while((JSON_STRINO=bufferReader.readLine())!=null){
stringBuilder.append(JSON_STRINO+"\n");
//add new textView
}
bufferReader.close();
inputStream.close();
httpUrlConnection.disconnect();
json_string=stringBuilder.toString().trim();
return stringBuilder.toString().trim();
// return "one row of data inserted..";
} catch (MalformedURLException e) {
error=e.getMessage()+" first";
} catch (IOException e) {
// TODO Auto-generated catch block
error=e.getMessage()+" sec";
}
return error;
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
json_string=result;
l=parse();
int i=0;
for(final String a[]:l){
TextView t=new TextView(MainActivity.this);
final String path=a[2]+".jpg";
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t.setText(a[0]+" : "+a[1]+" i="+i);
t.setId(i);
Reklam.addView(t);
new LoadImage().execute(path);
i++;
}}}
我想在依次加载第一个文本和第二个文本之后加载图像时加载图像,但它先加载所有文本,然后再加载所有图像,这些用于加载图像的代码如下:
private class LoadImage extends AsyncTask<String, String, Bitmap> {
Bitmap bitmap;
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
ImageView img=new ImageView(MainActivity.this);
img.setImageBitmap(image);
img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Reklam.addView(img);
pDialog.dismiss();
}else{
pDialog.dismiss();
Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
如果你想运行多线程并行:
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
new MyAsyncTask().execute();
}