2016-04-26 79 views
0

我有一個基於Web的應用程序後,一個文本retrived,使用下面的代碼retrive文本必須retrive在服務器外部數據庫和圖像文本:如何在同一時間內運行asynctask?

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(); 

     } 
    } 
} 

回答

0

,如果你想平行運行多個線程:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
    } else { 
     new MyAsyncTask().execute(); 
    }