Android Open Source - final_year_frontend Retriever






From Project

Back to project page final_year_frontend.

License

The source code is released under:

MIT License

If you think the Android project final_year_frontend 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 com.james.erebus.networking;
//from   w  w  w  .java 2  s.  c  o  m
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.UnknownHostException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import com.james.erebus.JSONJava.JSONArray;
import com.james.erebus.JSONJava.JSONException;
import com.james.erebus.JSONJava.JSONTokener;
import com.james.erebus.misc.AppConsts;

import android.content.Context;
import android.util.Log;

/**
 * Parent class that implements network retrieval of information from the main back-end server
 * @author james
 *
 */

public abstract class Retriever {

  protected final String baseUrl = "http://teamfrag.net:3001";

  /**
   * Method to retrieve information, first looking in the cache
   * @param uri The URI to fall back on if the cache is empty
   * @param filename The cache filename to look in
   * @return A {@link com.james.erebus.JSONJava.JSONArray} of the information that is returned
   */
  public JSONArray retrieve(URI uri, String filename)
  {

    try{
      File f = new File(AppConsts.currentActivity.getFilesDir() + "/" + filename);
      Log.v("newfilename", f.getCanonicalPath());
      boolean needToDownload = false;
      if(f.length() <= 2) //returns 0 if file doesnt exist or length is 0, or if the file contains an empty json array []
      {
        needToDownload = true;
        f.createNewFile(); //create a new file, doesn't matter here because it either doesn't exist or has nothing in it
      }
      if(!needToDownload)
      {
        FileInputStream fis = AppConsts.currentActivity.openFileInput(filename);
        JSONArray ja;
        int ch;
        StringBuffer strBuf = new StringBuffer("");
        while((ch = fis.read()) != -1)
        {
          strBuf.append((char)ch);
        }
        fis.close();
        ja = new JSONArray(strBuf.toString());
        return ja;
      }
      else
      {
        return forceRetrieveFromServer(uri, filename);
      }
    }  catch (IOException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    } 
    return null;
  }

  /**
   * Retrieves information straight from the server, bypassing the cache
   * @param uri The URI to retrieve from
   * @param filename The filename to write the the retrieved information into
   * @return A {@link com.james.erebus.JSONJava.JSONArray} of the retrieved information
   * @throws UnknownHostException
   */
  public JSONArray forceRetrieveFromServer(URI uri, String filename) throws UnknownHostException
  {
    try{

      HttpClient httpclient = new DefaultHttpClient();
      HttpParams httpParameters = httpclient.getParams();
      HttpConnectionParams.setTcpNoDelay(httpParameters, true); 
      HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
      HttpGet httpget = new HttpGet(uri);
      HttpResponse response = httpclient.execute(httpget);

      HttpEntity entity = response.getEntity();

      if (entity != null) {
        InputStream instream = entity.getContent();
        try {
          JSONTokener jt = new JSONTokener(instream);
          JSONArray retrievedInfo = new JSONArray(jt);
          FileOutputStream fos = AppConsts.currentActivity.openFileOutput(filename, Context.MODE_PRIVATE);
          fos.write(retrievedInfo.toString().getBytes());
          fos.close();
          return retrievedInfo;
        } finally {
          instream.close();
        }
      }

    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    } 
    return null;
  }
}




Java Source Code List

com.james.erebus.GCMIntentService.java
com.james.erebus.JSONJava.CDL.java
com.james.erebus.JSONJava.CookieList.java
com.james.erebus.JSONJava.Cookie.java
com.james.erebus.JSONJava.HTTPTokener.java
com.james.erebus.JSONJava.HTTP.java
com.james.erebus.JSONJava.JSONArray.java
com.james.erebus.JSONJava.JSONException.java
com.james.erebus.JSONJava.JSONML.java
com.james.erebus.JSONJava.JSONObject.java
com.james.erebus.JSONJava.JSONString.java
com.james.erebus.JSONJava.JSONStringer.java
com.james.erebus.JSONJava.JSONTokener.java
com.james.erebus.JSONJava.JSONWriter.java
com.james.erebus.JSONJava.XMLTokener.java
com.james.erebus.JSONJava.XML.java
com.james.erebus.core.C2DMRegistrationReceiver.java
com.james.erebus.core.CustomOnItemSelectedListener.java
com.james.erebus.core.MainActivity.java
com.james.erebus.core.MatchActivity.java
com.james.erebus.core.MatchButtonActivity.java
com.james.erebus.core.MatchOptions.java
com.james.erebus.core.MatchPreferencesFragment.java
com.james.erebus.core.Match.java
com.james.erebus.core.NotificationActivity.java
com.james.erebus.core.Notification.java
com.james.erebus.core.ParentPreferencesFragment.java
com.james.erebus.core.TournamentActivity.java
com.james.erebus.core.TournamentButtonActivity.java
com.james.erebus.core.TournamentFactory.java
com.james.erebus.core.TournamentPreferencesFragment.java
com.james.erebus.core.Tournament.java
com.james.erebus.core.TournyMatchOptions.java
com.james.erebus.misc.AppConsts.java
com.james.erebus.misc.MiscJsonHelpers.java
com.james.erebus.misc.misc.java
com.james.erebus.networking.AddDeviceTask.java
com.james.erebus.networking.AddMatchSubscriptionTask.java
com.james.erebus.networking.AddTournamentSubscriptionToServerTask.java
com.james.erebus.networking.GcmRegisterDeviceTask.java
com.james.erebus.networking.GetMatchesTask.java
com.james.erebus.networking.GetTournamentsTask.java
com.james.erebus.networking.MatchRetriever.java
com.james.erebus.networking.MatchSubscriptionManager.java
com.james.erebus.networking.MiscNetworkingHelpers.java
com.james.erebus.networking.NotificationManager.java
com.james.erebus.networking.RemoveMatchSubscriptionFromServerTask.java
com.james.erebus.networking.RemoveTournamentSubscriptionFromServerTask.java
com.james.erebus.networking.Retriever.java
com.james.erebus.networking.SubscriptionManager.java
com.james.erebus.networking.SubscriptionRetriever.java
com.james.erebus.networking.TournamentRetriever.java
com.james.erebus.networking.TournamentSubscriptionManager.java