title: How to implement ASyncTask in Android
body: <p>Can anybody tell me how to update the UI using ASyncTask and give an example?</p>
  
  <p>I am getting the value from two webservices. From two webservices I am getting value in two arraylist on post method will not return value it will rounding in loop. How do I return from onPostExecuteMethod?</p>
  
  <p>This is my async class:</p>
  
  <pre><code>public class HomeTask extends AsyncTask&lt;Void,Void,Bundle&gt; 
  { 
  
          private Context ctx; 
          public HomeTask(Context context) { 
              ctx = context; 
          } 
  
          @Override 
          protected void onPreExecute() { 
              //super.onPreExecute(); 
          //setContentView(R.layout.splash);
  
             dlg = new ProgressDialog(getDialogContext()); 
              dlg.setMessage("Loading...."); 
              dlg.show();
              //setContentView(R.layout.splash);
          } 
  
  
          @Override
          protected Bundle doInBackground(Void... params) {
                 Bundle b=new Bundle();
  
              // TODO Auto-generated method stub
              try
              {
  
  
                     WebService tableservice=new WebService();
                  tableservicevalue=tableservice.callTableServer(SOAP_ACTION,"",strUsername,strPassWord,questGroupId,URL);
                    tableservicevalue=decodeXMLData(tableservicevalue);
  
                    DomTableParser parser=new DomTableParser();
                   parser.setTableservicevalue(tableservicevalue);
                   parsedValue=parser.parseXmlFile(tableservicevalue);
                   Log.d("1234%%%%$$$$$$$parsed value$$$$$", parsedValue.toString());
                    WebService service=new WebService();
                    webservicevalue=service.callHomeServer(SOAP_ACTION,"",strUsername,strPassWord,questGroupId,URL);
                     webservicevalue=decodeXMLData(webservicevalue);
                      ArticleParser articleParser=new ArticleParser();
                 articleParsedValue=articleParser.parseXmlArticle(webservicevalue);
  
                  //b.putStringArrayList("articleParsedValue", articleParsedValue);
  
                  Log.d("(((((((parsed value is",parsedValue.toString());
  
                 b.putStringArrayList("parsedValue", parsedValue);
                 b.putStringArrayList("articleParsedValue", articleParsedValue);
                 Log.d("(((((((parsed value is",articleParsedValue.toString());
  
  
  
               }
              catch (Exception e) {
              // TODO: handle exception
                  e.printStackTrace();
          }
  
         return b;
          }                   
            @Override 
              protected void onPostExecute(Bundle b) { 
  
  
                Log.d("vijay checking","checking&amp;&amp;&amp;&amp;");
                                Log.d("****","*********");
                                Log.d("calling","handler");
                                Log.d("****","*********");
  
  
                   parsedValue1=b.getStringArrayList("parsedValue");
                   articleParsedValue1=b.getStringArrayList("articleParsedValue");                  
  
  } 
  </code></pre>
  
  <p>onupdating user interface</p>
  
  <pre><code>new HomeTask(HomeActivity2.this).execute(null); 
  
  for(int i=0;i&lt;parsedValue1.size();i++)
  {
                  DomParserTableDataSet dataSet=(DomParserTableDataSet)parsedValue1.get(i);  
  
                  if(i==0)
                  {
                      Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is", dataSet.getIndexName());
                      Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is", dataSet.getLastValue());
                      Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is",dataSet.getChangePercentage() );
                   txtindex0.setText(dataSet.getIndexName());
                   txtlast0.setText(dataSet.getLastValue());
                   txtchange0.setText(dataSet.getChangePercentage());
                  }
                  if(i==1)
                  {
                      Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is", dataSet.getIndexName());
                      Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is", dataSet.getLastValue());
                      Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is",dataSet.getChangePercentage() );
                      txtindex1.setText(dataSet.getIndexName());
                       txtlast1.setText(dataSet.getLastValue());
                      txtchange1.setText(dataSet.getChangePercentage());  
                   }
                 if(i==2)
                  {
                  Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is", dataSet.getIndexName());
                  Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is", dataSet.getLastValue());
                  Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is",dataSet.getChangePercentage() );
                   txtindex2.setText(dataSet.getIndexName());
                   txtlast2.setText(dataSet.getLastValue());
                   txtchange2.setText(dataSet.getChangePercentage()); 
                  }
  
              if(i==3)
              {
                  Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is", dataSet.getIndexName());
                  Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is", dataSet.getLastValue());
                  Log.d("&amp;&amp;&amp;&amp;&amp;&amp;index name is",dataSet.getChangePercentage() );
               txtindex3.setText(dataSet.getIndexName());
                   txtlast3.setText(dataSet.getLastValue());
                   txtchange3.setText(dataSet.getChangePercentage()); 
              }
  
            }
  
  
          //
        for(int i=0;i&lt;articleParsedValue1.size();i++)
         {
  
             System.out.println("for loop checking i is"+i);
                  ArticleDataSet articleDataset=(ArticleDataSet)articleParsedValue1.get(i);
  
             System.out.println("articleDataset.getArticle_title()"+articleDataset.getArticle_title());       
  
                  HashMap&lt;String, String&gt; mapValue=new HashMap&lt;String, String&gt;();  
                     WebView webviewcontent=new WebView(HomeActivity2.this);
               if(articleDataset.getArticle_summary().length()&gt;75)  
               {
                    summary=articleDataset.getArticle_summary().substring(0,75)+"...";
               }
               else
               {
                   summary=articleDataset.getArticle_summary();
               }
  
  
                 String html ="&lt;html&gt;&lt;body&gt;&lt;div&gt;&lt;label&gt; &lt;font face=\"verdana\"  color=\"#C1002B\" size=\"4\"&gt;&lt;b&gt;"+articleDataset.getArticle_title()+"&lt;/b&gt; &lt;/font&gt; &lt;/label&gt;"+ "&lt;label&gt; &lt;font color=\"#000000\" size=\"2\" face=\"verdana\"&gt;"+"|"+"&lt;/font&gt;&lt;/label&gt; "+"&lt;label&gt; &lt;font color=\"#AAAAAA\" face=\"verdana\" size=\"2\"&gt;"+articleDataset.getArticle_date()+" &lt;/font&gt;&lt;/label&gt;&lt;/div&gt;";
            html=html+"&lt;div&gt;&lt;label&gt; &lt;font color=\"#000000\" face=\"verdana\" size=\"2\"&gt;"+summary+" &lt;/font&gt;&lt;/label&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;" ;
               webviewcontent.getSettings().setJavaScriptEnabled(true);
            webviewcontent.clearCache(true);
            final Activity activity = HomeActivity2.this;
  
  
            webviewcontent.loadData(html, "text/html", "utf-8");
  
             if(i==articleParsedValue1.size()-1)
                        {
            webviewcontent.setWebViewClient(new WebViewClient() 
            {
                public boolean shouldOverrideUrlLoading(WebView view, String url) 
                {              
                    view.loadUrl(url);
                    return true;          
                }
  
                public void onPageFinished(WebView view, String url)
                {
  
                   // setContentView(R.layout.home);   
  
                  if (dlg.isShowing())
                    {
  
                     dlg.dismiss();
                     dlg = null;
  
  
                    }
                  splash.setVisibility(View.GONE);
  
                 // setContentView(repl);
  
  
                }                 
             }); 
            } 
            mainlinear4.addView(webviewcontent);
               //webviewcontent.setOnTouchListener(HomeActivity2.this);                
  
          }
  
  }
  </code></pre>