Android Open Source - on-the-hook Request Activity






From Project

Back to project page on-the-hook.

License

The source code is released under:

MIT License

If you think the Android project on-the-hook 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.yoandinkov.onthehook;
/*from w  w  w  . j  a v a2s.  co  m*/
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.yoandinkov.onthehook.models.HttpResponseWrapper;
import com.yoandinkov.onthehook.models.HttpType;
import com.yoandinkov.onthehook.models.RequestPurpose;
import com.yoandinkov.onthehook.models.Response;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public abstract class RequestActivity extends Activity implements OnClickListener {
  /**
   * Session key is stored from login until logout every request except
   * register or login requires session key value as header.
   */
  private String sessionKey;

  /** Common url for every request on server. */
  private String apiUrl = "http://on-the-hook.herokuapp.com/api";

  // common methods

  protected abstract void handleResponse(HttpResponseWrapper response);

  public abstract void onClick(View v);
  
  // Http Requesters
  
  private String getCredentials() {
    String result = "";
    
    SharedPreferences credentials = getSharedPreferences("credentials", MODE_PRIVATE);
    
    result = credentials.getString("sessionKey", null);
    
    return result;
  }
  
  protected class HttpGetRequester extends AsyncTask<String, Boolean, Response> {
    private RequestPurpose currentPurpose;
    
    @Override
    protected Response doInBackground(String... params) {
      Response currentResponse = new Response();

      HttpClient httpClient = new DefaultHttpClient();

      try {
        String url = params[0];
        
        HttpGet currentRequest = new HttpGet(url);
        
        currentRequest.addHeader("content-type", "application/json");
        currentRequest.addHeader("x-SessionKey", RequestActivity.this.getCredentials());
        
        HttpResponse response = httpClient.execute(currentRequest);

        InputStream responseInputStream = response.getEntity()
            .getContent();
        InputStreamReader responseInputStreamReader = new InputStreamReader(
            responseInputStream);
        BufferedReader responseBufferedReader = new BufferedReader(
            responseInputStreamReader);
        
        String chunk = null;
        StringBuilder responseBody = new StringBuilder();

        while ((chunk = responseBufferedReader.readLine()) != null) {
          responseBody.append(chunk);
        }

        currentResponse.setBody(responseBody.toString());
        currentResponse.setStatusCode(response.getStatusLine()
            .getStatusCode());

      } catch (Exception ex) {
        Log.e("OnTheHook", ex.getMessage());
      } finally {
        httpClient.getConnectionManager().shutdown();
      }

      return currentResponse;
    }

    @Override
    protected void onPostExecute(Response result) {
      super.onPostExecute(result);

      HttpResponseWrapper currentResponse = new HttpResponseWrapper();
      
      currentResponse.setPurpose(this.getCurrentPurpose());
      currentResponse.setType(HttpType.GET);
      currentResponse.setResponse(result);

      RequestActivity.this.handleResponse(currentResponse);
    }

    public RequestPurpose getCurrentPurpose() {
      return currentPurpose;
    }

    public void setCurrentPurpose(RequestPurpose currentPurpose) {
      this.currentPurpose = currentPurpose;
    }
  }

  protected class HttpPutRequester extends AsyncTask<String, Boolean, Response> {
    private RequestPurpose currentPurpose;

    @Override
    protected Response doInBackground(String... params) {
      Response currentResponse = new Response();

      HttpClient httpClient = new DefaultHttpClient();

      try {
        String url = params[0];

        HttpPut currentRequest = new HttpPut(url);

        String requestBodyJson = params[1];
        
        StringEntity requestBody = new StringEntity(requestBodyJson);

        currentRequest.addHeader("content-type", "application/json");
        currentRequest.addHeader("x-SessionKey", RequestActivity.this.getCredentials());
        currentRequest.setEntity(requestBody);

        HttpResponse response = httpClient.execute(currentRequest);

        InputStream responseInputStream = response.getEntity()
            .getContent();
        InputStreamReader responseInputStreamReader = new InputStreamReader(
            responseInputStream);
        BufferedReader responseBufferedReader = new BufferedReader(
            responseInputStreamReader);

        String chunk = null;
        StringBuilder responseBody = new StringBuilder();

        while ((chunk = responseBufferedReader.readLine()) != null) {
          responseBody.append(chunk);
        }

        currentResponse.setBody(responseBody.toString());
        currentResponse.setStatusCode(response.getStatusLine()
            .getStatusCode());

      } catch (Exception ex) {
        Log.e("OnTheHook", ex.getMessage());
      } finally {
        httpClient.getConnectionManager().shutdown();
      }

      return currentResponse;
    }

    @Override
    protected void onPostExecute(Response result) {
      super.onPostExecute(result);

      HttpResponseWrapper currentResponse = new HttpResponseWrapper();

      currentResponse.setResponse(result);
      currentResponse.setType(HttpType.PUT);
      currentResponse.setPurpose(this.getCurrentPurpose());
      
      RequestActivity.this.handleResponse(currentResponse);
    }

    public RequestPurpose getCurrentPurpose() {
      return currentPurpose;
    }

    public void setCurrentPurpose(RequestPurpose currentPurpose) {
      this.currentPurpose = currentPurpose;
    }
  }

  protected class HttpPostRequester extends AsyncTask<String, Boolean, Response> {
    private RequestPurpose currentPurpose;

    @Override
    protected Response doInBackground(String... params) {
      Response currentResponse = new Response();

      HttpClient httpClient = new DefaultHttpClient();

      try {
        String url = params[0];

        HttpPost currentRequest = new HttpPost(url);

        String requestBodyJson = params[1];
        
        StringEntity requestBody = new StringEntity(requestBodyJson);

        currentRequest.addHeader("content-type", "application/json");
        currentRequest.addHeader("x-SessionKey", RequestActivity.this.getCredentials                          ());
        currentRequest.setEntity(requestBody);

        HttpResponse response = httpClient.execute(currentRequest);

        InputStream responseInputStream = response.getEntity()
            .getContent();
        InputStreamReader responseInputStreamReader = new InputStreamReader(
            responseInputStream);
        BufferedReader responseBufferedReader = new BufferedReader(
            responseInputStreamReader);

        String chunk = null;
        StringBuilder responseBody = new StringBuilder();

        while ((chunk = responseBufferedReader.readLine()) != null) {
          responseBody.append(chunk);
        }

        currentResponse.setBody(responseBody.toString());
        currentResponse.setStatusCode(response.getStatusLine()
            .getStatusCode());

      } catch (Exception ex) {
        Log.e("OnTheHook", ex.getMessage());
      } finally {
        httpClient.getConnectionManager().shutdown();
      }

      return currentResponse;
    }

    @Override
    protected void onPostExecute(Response result) {
      super.onPostExecute(result);

      HttpResponseWrapper currentResponse = new HttpResponseWrapper();

      currentResponse.setResponse(result);
      currentResponse.setType(HttpType.POST);
      currentResponse.setPurpose(this.getCurrentPurpose());

      RequestActivity.this.handleResponse(currentResponse);
    }

    public RequestPurpose getCurrentPurpose() {
      return currentPurpose;
    }

    public void setCurrentPurpose(RequestPurpose currentPurpose) {
      this.currentPurpose = currentPurpose;
    }
  }
  
  // Auto-generated getters/setters

  protected String getApiUrl() {
    return apiUrl;
  }

  protected void setApiUrl(String apiUrl) {
    this.apiUrl = apiUrl;
  }

  protected String getSessionKey() {
    SharedPreferences credentials = getSharedPreferences("credentials", MODE_PRIVATE);
    
    String sessionKey = credentials.getString("sessionKey", null);
    
    return sessionKey;
  }

  protected void setSessionKey(String sessionKey) {
    this.sessionKey = sessionKey;
  }
}




Java Source Code List

com.yoandinkov.onthehook.DraughtActivity.java
com.yoandinkov.onthehook.FishActivity.java
com.yoandinkov.onthehook.FishingActivity.java
com.yoandinkov.onthehook.LoginActivity.java
com.yoandinkov.onthehook.MainMenuActivity.java
com.yoandinkov.onthehook.RegisterActivity.java
com.yoandinkov.onthehook.RequestActivity.java
com.yoandinkov.onthehook.adapters.FishAdapter.java
com.yoandinkov.onthehook.crypt.JsonConverter.java
com.yoandinkov.onthehook.crypt.SHA1Converter.java
com.yoandinkov.onthehook.db.DatabaseHelper.java
com.yoandinkov.onthehook.db.DatabaseManager.java
com.yoandinkov.onthehook.db.models.FishDbModel.java
com.yoandinkov.onthehook.models.Coordinates.java
com.yoandinkov.onthehook.models.Credentials.java
com.yoandinkov.onthehook.models.FishButton.java
com.yoandinkov.onthehook.models.FishSpecies.java
com.yoandinkov.onthehook.models.Fish.java
com.yoandinkov.onthehook.models.Fishing.java
com.yoandinkov.onthehook.models.HttpResponseWrapper.java
com.yoandinkov.onthehook.models.HttpType.java
com.yoandinkov.onthehook.models.Login.java
com.yoandinkov.onthehook.models.NewFishing.java
com.yoandinkov.onthehook.models.Registration.java
com.yoandinkov.onthehook.models.RequestPurpose.java
com.yoandinkov.onthehook.models.Response.java
com.yoandinkov.onthehook.models.WaterLocation.java