I am having two tasks running in doInBackground(). One task is reading data from site and another task is parsing xml file from Url and set image in imageview. But i want to separate both tasks means once the reading from the site is completed , it should set text in textview. and after that xml parsing should start. So what can i do ?
Here is my code :
private class DownloadWebPageTask extends AsyncTask<String, Void, ArrayList<String>>
{
ProgressBar progressBar;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar = (ProgressBar)findViewById(R.id.showLoading);
progressBar.bringToFront();
}
@Override
protected ArrayList<String> doInBackground(String... arg0)
{
String xml = null;
ArrayList<String> lyricArtistArray = new ArrayList<String>();
//get lyrics - First task
lyricsArray = SearchHelper.findLyrics(arg0[0],arg0[1]);
lyricArtistArray.add(lyricsArray);
//get image - Second task
artistNameGlobal = arg0[1].toString();
try
{
XMLParser parser= new XMLParser();
String img;
String t = arg0[1].replace(" ","%20");
String url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="+t+"&api_key=mykey";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName("artist");
for(int i=0;i<1;i++)
{
Element e = (Element)nl.item(i);
img = parser.getValue(e,"image");
System.out.print(img);
ImageView imageArtist = (ImageView) findViewById(R.id.artistImageView);
imageArtist.setImageBitmap(LoadImageFromURL(img));
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return lyricArtistArray;
}
@Override
protected void onPostExecute(ArrayList<String> lyricArtistArray)
{
progressBar.setVisibility(View.GONE);
tvMain.setTextSize(fontSize);
tvMain.setTextColor(FontColor);
if(!FontStyle.equals("None"))
{
String s = FontStyle.concat(".ttf");
String s1 = "fonts/"+s;
Typeface tf=Typeface.createFromAsset(getAssets(),s1);
tvMain.setTypeface(tf);
}
tvMain.setText(Html.fromHtml(lyricArtistArray.get(0).toString()));
tvMain.setMovementMethod(new ScrollingMovementMethod());
tvMain.setSelected(true);
}
}
How should i handle these two tasks ? Please suggest me some solution so that it takes minimum time. Please reply me fast. Thanx in advance.
A couple of approaches come to mind. The first is to implement
onProgressUpdate(Progress...)passing aStringand set the value of theTextViewwithin this method. You would then callupdateProgressafter your download is complete, but before the parse starts. This would be the minimum amount of work on you part to get this working.The other route is to create a new
AsyncTaskwith just the parse code in it. The firstAsyncTask(which would now just perform the download) will set theTextViewcontent and start the parsingAsyncTaskin itsonPostExecute.