Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Created February 9, 2011 09:04
Show Gist options
  • Save typeoneerror/818180 to your computer and use it in GitHub Desktop.
Save typeoneerror/818180 to your computer and use it in GitHub Desktop.
abstract class to run in non-UI thread, and load an XML file into value objects
/*
* Copyright (c) 2011 Typeoneerror Studios LLC
* $Id$
*/
package com.typeoneerror.apps.application.tasks;
import android.util.Log;
import com.typeoneerror.apps.application.io.ActivityFeedHandler;
import com.typeoneerror.apps.application.model.Model;
import com.typeoneerror.apps.application.utils.Constants;
import com.typeoneerror.apps.application.utils.HttpHelper;
import java.util.List;
/**
* Using the AsyncTaskWithHandler to load recent activity in
* a thread that isn't UI.
*/
public class ActivityAsyncTask extends AsyncTaskWithHandler
{
private static final String TAG = Constants.TAG + ActivityAsyncTask.class.getSimpleName();
public ActivityAsyncTask(AsyncTaskCompleteListener<List<Model>> activity)
{
super(activity);
setHandler(new ActivityFeedHandler());
}
@Override
protected List<Model> doInBackground(String... params)
{
// load xml
final HttpHelper helper = new HttpHelper();
String response = helper.makeGetRequest("http://localhost/activity.xml");
List<Model> result = null;
try
{
parseResultUsingHandler(response);
result = getHandler().getList();
Log.i(TAG, "Number of events: " + result.size());
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
}
/*
* Copyright (c) 2011 Typeoneerror Studios LLC
* $Id$
*/
package com.typeoneerror.apps.application.tasks;
import android.os.AsyncTask;
import android.util.Log;
import com.typeoneerror.apps.application.io.BasicDefaultHandler;
import com.typeoneerror.apps.application.model.Model;
import com.typeoneerror.apps.application.utils.Constants;
import java.io.StringReader;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/**
* An asynchronous task implementation using commonsguy's
* technique for attaching and detaching the activity reference
* so as to avoid a stranded reference to the context.
* <p/>
* After XML is loaded, it is parsed into a List of Models.
* <p/>
* https://github.com/commonsguy/cw-android/blob/master/Rotation/RotationAsync/src/com/commonsware/android/rotation/async/RotationAsync.java
*
* @author typeoneerror
* @version 1.0
*/
abstract public class AsyncTaskWithHandler extends AsyncTask<String, Void, List<Model>>
{
private static final String TAG = Constants.TAG + AsyncTaskWithHandler.class.getSimpleName();
private AsyncTaskCompleteListener<List<Model>> activity;
private BasicDefaultHandler handler;
private boolean completed;
public AsyncTaskWithHandler(AsyncTaskCompleteListener<List<Model>> activity)
{
attach(activity);
}
public void attach(AsyncTaskCompleteListener<List<Model>> activity)
{
this.activity = activity;
}
public void detach()
{
activity = null;
}
public BasicDefaultHandler getHandler()
{
return handler;
}
public boolean isCompleted()
{
return completed;
}
public void setHandler(BasicDefaultHandler handler)
{
this.handler = handler;
}
@Override
protected void onPostExecute(List<Model> result)
{
completed = true;
activity.onTaskComplete(result);
}
protected void parseResultUsingHandler(String result) throws Exception
{
if (handler == null)
{
throw new Exception("You must set a handler to parse XML results.");
}
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler((DefaultHandler) handler);
InputSource source = new InputSource();
source.setCharacterStream(new StringReader(result));
reader.parse(source);
}
catch (Exception e)
{
Log.e(TAG, "XML Parsing Exception: " + e);
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment