I have an asynctask that I am using to parse JSON and XML data during my ocCreate method. The problem is that the Async Task only finishes it’s “do in background” work once out of every ten times. When it fails to complete it’s background work, the app crashes due to variables the weren’t instantiated in the background process and are referenced in post-execute. This is very strange, because when the AsyncTask finishes it background work everythig works fine. Any help is greatly appreciated
My Code
private class LoadingData extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
//TWITTER
//Parses Twitter XML with no problem...
publishProgress(15,1);
//SPOT GOLD AND SILVER
try{
GoldSpotPriceReader.feed = "http://feed43.com/437361835043234.xml";//Gold
GoldSpotPriceReader.getLatestRssFeed();
livespotgold=GoldSpotPriceReader.spotprice;
storedspotgold=livespotgold;
SilverSpotPriceReader.feed = "http://feed43.com/824383110654537.xml";//Silver
SilverSpotPriceReader.getLatestRssFeed();
livespotsilver=SilverSpotPriceReader.spotprice;
storedspotsilver=livespotsilver;
}catch(NumberFormatException e){
Log.e("Spot Price Initial", "Spot Price Format Error "+e.toString());
}
//AsyncTask Usually Ends Here For No Particular Reason.
//And doesn't finish background work...
publishProgress(30,2);
//More background work...
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog (MyActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("Fetching Latest News");
dialog.setIndeterminate(false);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
finddealer = (MapView) findViewById(R.id.mvFindDealer);
finddealer.setBuiltInZoomControls(true);
Touchy tango = new Touchy();
overlayList = finddealer.getOverlays();
overlayList.add(tango);
compass = new MyLocationOverlay(MyActivity.this, finddealer);
overlayList.add(compass);
controller = finddealer.getController();
controller.setZoom(11);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(lm.GPS_PROVIDER, 300000, 5000, MyActivity.this);
crit = new Criteria();
towers = lm.getBestProvider(crit, false);
location = lm.getLastKnownLocation(lm.GPS_PROVIDER);
delta = getResources().getDrawable(R.drawable.map_pin_48);
zulu = getResources().getDrawable(R.drawable.ic_launcher);
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
dialog.setProgress(progress[0]);
switch (progress[1])
{
case 1:
dialog.setMessage("Acquiring Prices...");
break;
case 2:
dialog.setMessage("Acquiring Rates...");
break;
case 3:
dialog.setMessage("Acquiring More Rates...");
break;
case 4:
dialog.setMessage("Pinpointing Your Location...");
break;
case 5:
dialog.setMessage("Locating Nearest Dealers...");
break;
case 6:
dialog.setMessage("Calculating Shortest Routes...");
break;
case 7:
dialog.setMessage("Mapping Dealers...");
break;
case 8:
dialog.setMessage("Finalizing...");
break;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.setProgress(100);
dialog.setMessage("Load Complete.");
dialog.dismiss();
//Other UIthread variables...
}
I have had similar situation before with my AsyncTask. the problem was that I was catching an exception and trying to throw a toast without a handler and runnable. So make sure you not trying to do anything on the UIThread within do-in-background process.