Android Open Source - SandB-Android Xml Fetch Task






From Project

Back to project page SandB-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project SandB-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.grinnell.sandb.xmlpull;
/*w  w  w  .j a v a2  s .com*/
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.ParseException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;
import edu.grinnell.sandb.xmlpull.XmlFetchTask.Result;

public class XmlFetchTask extends AsyncTask<String, Void, Result> {

  private Context mAppContext;
  private RetrieveDataListener mRetrieveDataListener;
    
  public static final String TAG   = "XmlFetchTask";
  
  public XmlFetchTask(Context context, RetrieveDataListener rdl) {
    super();
    mAppContext = context;
    mRetrieveDataListener = rdl;    
  }

  /* Setup the progress bar. */
  @Override
  protected void onPreExecute() {
    // Progress bar?
  }
  
  @Override
  protected Result doInBackground(String... arg0) {
    ConnectivityManager cm = (ConnectivityManager)
        mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    
    Result r = new Result();
    
    if (!networkEnabled(cm)) 
      return (r.setCode(Result.NO_NETWORK));
    
    InputStream stream = downloadDataFromServer(arg0[0]);

    if (stream == null)
      return r.setCode(Result.NO_DATA);
    r.setStream(stream);
    
    return r.setCode(Result.SUCCESS);
  }
  
  /* Stop the dialog and notify the main thread that the new menu
   * is loaded. */
  @Override
  protected void onPostExecute(Result result) {
    super.onPostExecute(result);
    // notify the UI thread listener ..
    mRetrieveDataListener.onRetrieveData(result);
  }
  
  /* Return true if the device has a network adapter that is capable of 
   * accessing the network. */
  protected static boolean networkEnabled(ConnectivityManager cm) {
    NetworkInfo n = cm.getActiveNetworkInfo();
    return (n != null) && n.isConnectedOrConnecting();
  }
  
  protected static InputStream downloadDataFromServer(String urlstr) {
    InputStream stream = null;
    try {
      URL url = new URL(urlstr);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setReadTimeout(10000 /* milliseconds */);
          conn.setConnectTimeout(15000 /* milliseconds */);
          conn.setRequestMethod("GET");
          conn.setDoInput(true);
          // Starts the query
          conn.connect();
          stream = conn.getInputStream();
    } catch (IOException e) {
      Log.e(TAG, "exception: " + e.toString());
      Log.e(TAG, "message: " + e.getMessage());
    } catch (ParseException p) {
      Log.e(TAG, "ParseException: " + p.toString());} 
    
    return stream;
  }

  
  /* Listener you should implement for the callback method in the UI thread
   * and pass to the constructor of GetMenuTask */
  public interface RetrieveDataListener {
    public void onRetrieveData(Result result);
  }
  
  public class Result {
    
    /* Result Code Constants */
    public static final int UNKNOWN = -1;
    public static final int SUCCESS = 0;
    public static final int NO_NETWORK = 1;
    public static final int NO_ROUTE = 2;
    public static final int HTTP_ERROR = 3;
    public static final int NO_CACHE = 4;
    public static final int NO_DATA = 5;
    
    private int code;
    private InputStream stream;
    
    public Result (int resultCode, InputStream resultStream) {
      code = resultCode;
      stream = resultStream;
    }
    public Result () {this(-1, null);}
    public int getCode() {return code;}
    public InputStream getStream() {return stream;}
    public Result setCode(int c) {code = c; return this;}
    public Result setStream(InputStream s) {stream = s; return this;}
  }
}




Java Source Code List

edu.grinnell.sandb.ArticleDetailActivity.java
edu.grinnell.sandb.ArticleDetailFragment.java
edu.grinnell.sandb.ArticleListAdapter.java
edu.grinnell.sandb.ArticleListFragment.java
edu.grinnell.sandb.CommentListAdapter.java
edu.grinnell.sandb.CommentListFragment.java
edu.grinnell.sandb.ImagePagerActivity.java
edu.grinnell.sandb.MainActivity.java
edu.grinnell.sandb.MainPrefs.java
edu.grinnell.sandb.ScarletAndBlackApplication.java
edu.grinnell.sandb.Utility.java
edu.grinnell.sandb.comments.CommentStorageHelper.java
edu.grinnell.sandb.comments.CommentTable.java
edu.grinnell.sandb.comments.Comment.java
edu.grinnell.sandb.data.ArticleStorageHelper.java
edu.grinnell.sandb.data.ArticleTable.java
edu.grinnell.sandb.data.Article.java
edu.grinnell.sandb.img.BodyImageGetter.java
edu.grinnell.sandb.img.ImageStorageHelper.java
edu.grinnell.sandb.img.ImageTable.java
edu.grinnell.sandb.img.Image.java
edu.grinnell.sandb.img.UniversalLoaderUtility.java
edu.grinnell.sandb.xmlpull.CommentParseTask.java
edu.grinnell.sandb.xmlpull.XmlCheckAgeTask.java
edu.grinnell.sandb.xmlpull.XmlFetchTask.java
edu.grinnell.sandb.xmlpull.XmlParseTask.java
edu.grinnell.sandb.xmlpull.XmlPullReceiver.java
edu.grinnell.sandb.xmlpull.XmlPullService.java