Android Open Source - final_year_frontend Misc Networking Helpers






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;
/*ww w.jav a  2s .  co  m*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
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.JSONObject;
import com.james.erebus.misc.AppConsts;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

/**
 * A helper class which provides the necessary methods to POST and DELETE information to and from the server
 * and to add information to internal files
 * @author james
 *
 */

public class MiscNetworkingHelpers {

  public static String regId;

  public static Handler handler = new Handler(Looper.getMainLooper());

  
  /**
   * Adds a {@link com.james.erebus.JSONJava.JSONObject} to a file on the internal storage
   * @param obj The object to be added
   * @param filename The name of the file which will be added to
   */
  public static void addEntryToInternalStorage(JSONObject obj, String filename)
  {
    try{
      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());
      
      boolean doesObjectExist = false;
      for(int i = 0; i < ja.length(); i++)
      {
        if(ja.getJSONObject(i).get("id").equals(obj.get("id")))
        {
          doesObjectExist = true;
          ja.put(i, obj);
        }
      }
      if(!doesObjectExist)
      {  
        ja.put(obj);
        
      }
      FileOutputStream fos = AppConsts.currentActivity.openFileOutput(filename, Context.MODE_PRIVATE);
      fos.write(ja.toString().getBytes());
      fos.close();
    }
    catch (JSONException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * Posts information to the server
   * @param regId The registration ID of this app
   * @param uriExtension The URI extension to post to in String form
   * @param info The information to be posted
   * @return If the information was posted successfully
   * @throws Exception
   */
  public static boolean postInformationToServer(String regId, String uriExtension, ArrayList<BasicNameValuePair> info) throws Exception
  {
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams httpParameters = httpclient.getParams();
    HttpConnectionParams.setTcpNoDelay(httpParameters, true); 
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost("http://teamfrag.net:3002/" + uriExtension);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(info.size());
    for(BasicNameValuePair p : info)
      nameValuePairs.add(p);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    Log.v("response_code", Integer.toString(response.getStatusLine().getStatusCode()));
    if(response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300)
    {
      Log.v("postInformationToServer", "success!");
      return true;
    }
    else
    {
      Log.v("postInformationToServer", "failure :(");
      return false;
    }
  }

  /**
   * Deletes information from the server
   * @param regId The registration ID of this app
   * @param uriExtension The URI extension to post to
   * @return If the information was deleted successfully
   * @throws Exception
   */
  public static boolean deleteInformationFromServer(String regId, String uriExtension) throws Exception
  {
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams httpParameters = httpclient.getParams();
    HttpConnectionParams.setTcpNoDelay(httpParameters, true); 
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);

    HttpDelete httpdelete = new HttpDelete("http://teamfrag.net:3002/" + uriExtension);

    HttpResponse response = httpclient.execute(httpdelete);
    Log.v("response_code", Integer.toString(response.getStatusLine().getStatusCode()));
    if(response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300)
    {
      Log.v("deleteInformationFromServer", "success!");
      return true;
    }
    else
    {
      Log.v("deleteInformationFromServer", "failure :(");
      return false;
    }
  } 
}




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