org.hermit.android.net
Class WebFetcher

java.lang.Object
  extended by java.lang.Thread
      extended by org.hermit.android.net.WebFetcher
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
FileFetcher, TableFetcher

public abstract class WebFetcher
extends java.lang.Thread

This class fetches data from the web without blocking the main app.


Nested Class Summary
static class WebFetcher.FetchException
          Web fetching exception.
static interface WebFetcher.Listener
          Listener for incoming web data.
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  WebFetcher.Listener dataClient
           
protected  java.net.URL[] dataUrls
           
protected  boolean killed
           
protected  long newerThanDate
           
protected  long timeout
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
WebFetcher(java.net.URL[] urls, WebFetcher.Listener client, long timeout, long newer)
          Create a fetcher to get data from a given list of URLs.
WebFetcher(java.net.URL url, WebFetcher.Listener client, long timeout)
          Create a fetcher to get data from a given URL.
WebFetcher(java.net.URL url, WebFetcher.Listener client, long timeout, long newer)
          Create a fetcher to get data from a given URL.
 
Method Summary
protected  void fetch(java.net.URL url, long newer)
          Fetch an object from the given URL.
protected  void handle(java.net.URL url, java.net.URLConnection conn, java.io.BufferedReader stream)
          Handle data from the given BufferedReader.
protected  void handle(java.net.URL url, java.net.URLConnection conn, java.io.InputStream stream)
          Handle data from the given stream.
 void kill()
          Kill this fetcher.
static void killAll()
          Stop all fetch operations.
static void queue(WebFetcher fetcher)
          Queue a web fetch.
 void run()
          Thread's main method.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

timeout

protected final long timeout

killed

protected boolean killed

dataUrls

protected final java.net.URL[] dataUrls

newerThanDate

protected final long newerThanDate

dataClient

protected final WebFetcher.Listener dataClient
Constructor Detail

WebFetcher

public WebFetcher(java.net.URL url,
                  WebFetcher.Listener client,
                  long timeout)
Create a fetcher to get data from a given URL. Data will be fetched asynchronously from the URL, and passed as it arrives to the given client. You can start the fetch when you want by calling start(); or you can pass it to queue() to be executed when other fetches are done.

Parameters:
url - The URL to fetch data from.
client - Client to pass the data to.
timeout - Maximum time in ms for which the job will be allowed to run.

WebFetcher

public WebFetcher(java.net.URL url,
                  WebFetcher.Listener client,
                  long timeout,
                  long newer)
Create a fetcher to get data from a given URL. Data will be fetched asynchronously from the URL, and passed as it arrives to the given client. You can start the fetch when you want by calling start(); or you can pass it to queue() to be executed when other fetches are done.

Parameters:
url - The URL to fetch data from.
client - Client to pass the data to.
timeout - Maximum time in ms for which the job will be allowed to run.
newer - If-modified-since time in ms UTC. The fetch will only be carried out if the remote resource has been modified since this time. If zero, fetch without this condition.

WebFetcher

public WebFetcher(java.net.URL[] urls,
                  WebFetcher.Listener client,
                  long timeout,
                  long newer)
Create a fetcher to get data from a given list of URLs. Data will be fetched asynchronously from each URL in sequence, and passed as it arrives to the given client. If any URL fails, the whole fetch will be aborted. If all URLs succeed, onWebDone() will be called in the listener. You can start the fetch when you want by calling start(); or you can pass it to queue() to be executed when other fetches are done.

Parameters:
urls - The URLs to fetch data from.
client - Client to pass the data to.
timeout - Maximum time in ms for which the job will be allowed to run.
newer - If-modified-since time in ms UTC. The fetch will only be carried out if the remote resource has been modified since this time. If zero, fetch without this condition.
Method Detail

queue

public static void queue(WebFetcher fetcher)
Queue a web fetch. It will be executed when the current fetches are done.

Parameters:
fetcher - The web fetcher to queue.

killAll

public static void killAll()
Stop all fetch operations.


kill

public void kill()
Kill this fetcher. Don't start it, don't do any more callbacks.


run

public void run()
Thread's main method. Just fetch the URLs in sequence. If one fails, stop there.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

fetch

protected void fetch(java.net.URL url,
                     long newer)
              throws WebFetcher.FetchException,
                     java.io.IOException
Fetch an object from the given URL.

Parameters:
url - The URL to fetch.
newer - If-modified-since time in ms UTC. The fetch will only be carried out if the remote resource has been modified since this time. If zero, fetch without this condition.
Throws:
WebFetcher.FetchException - Some problem was detected, such as a timeout.
java.io.IOException - An I/O error occurred.

handle

protected void handle(java.net.URL url,
                      java.net.URLConnection conn,
                      java.io.InputStream stream)
               throws WebFetcher.FetchException,
                      java.io.IOException
Handle data from the given stream.

Parameters:
url - The URL we're reading.
conn - The current connection to the URL.
stream - The InputStream to read from.
Throws:
WebFetcher.FetchException - Some problem was detected, such as a timeout.
java.io.IOException - An I/O error occurred.

handle

protected void handle(java.net.URL url,
                      java.net.URLConnection conn,
                      java.io.BufferedReader stream)
               throws WebFetcher.FetchException,
                      java.io.IOException
Handle data from the given BufferedReader.

Parameters:
url - The URL we're reading.
conn - The current connection to the URL.
stream - The BufferedReader to read from.
Throws:
WebFetcher.FetchException - Some problem was detected, such as a timeout.
java.io.IOException - An I/O error occurred.