27 December, 2013

Java REST API HTTP Helper

To ease up on HTTP calls I've build a helper that takes care of their inner workings.

package com.milagaia.helper;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
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.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

public class HttpHelper {

 protected static final String UTF8 = "UTF-8";
 protected static final String CONTENT_TYPE = "application/json; charset=utf-8";
 
 public InputStream Post(String url, String content, int timeoutMillis) 
   throws ConnectTimeoutException, UnsupportedEncodingException, ClientProtocolException, IOException {

  InputStream result = null;

  DefaultHttpClient httpClient = buildHttpClient(timeoutMillis);
  HttpUriRequest req = buildPost(url, content);

  HttpResponse httpResponse = httpClient.execute(req);

  HttpEntity httpEntity = httpResponse.getEntity();
  result = httpEntity.getContent();

  return result;
 }

 public boolean Put(String url, String content, int timeoutMillis) 
   throws ConnectTimeoutException, UnsupportedEncodingException, ClientProtocolException, IOException {

  DefaultHttpClient httpClient = buildHttpClient(timeoutMillis);
  HttpUriRequest req = buildPut(url, content);

  HttpResponse httpResponse = httpClient.execute(req);

  int statusCode = httpResponse.getStatusLine().getStatusCode();

  return statusCode == HttpStatus.SC_OK;
 }

 public InputStream Get(String url, int timeoutMillis) 
   throws ConnectTimeoutException, UnsupportedEncodingException, ClientProtocolException, IOException {

  InputStream result = null;

  DefaultHttpClient httpClient = buildHttpClient(timeoutMillis);
  HttpUriRequest req = buildGet(url);

  HttpResponse httpResponse = httpClient.execute(req);

  HttpEntity httpEntity = httpResponse.getEntity();
  result = httpEntity.getContent();

  return result;
 }

 public InputStream Delete(String url, int timeoutMillis) 
   throws ConnectTimeoutException, UnsupportedEncodingException, ClientProtocolException, IOException {

  InputStream result = null;

  DefaultHttpClient httpClient = buildHttpClient(timeoutMillis);
  HttpUriRequest req = buildDelete(url);

  HttpResponse httpResponse = httpClient.execute(req);

  int statusCode = httpResponse.getStatusLine().getStatusCode();

  if (statusCode == HttpStatus.SC_OK) {

   HttpEntity httpEntity = httpResponse.getEntity();
   result = httpEntity.getContent();
  }

  return result;
 }

 private DefaultHttpClient buildHttpClient(int timeoutMillis) {

  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpParams httpParameters = httpClient.getParams();
  HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutMillis);
  HttpConnectionParams.setSoTimeout(httpParameters, timeoutMillis);

  return httpClient;
 }

 private HttpPost buildPost(String url, String content) 
   throws UnsupportedEncodingException {

  HttpPost httpPost = new HttpPost(url);
  ByteArrayEntity baEntity = new ByteArrayEntity(content.getBytes(UTF8));
  baEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, CONTENT_TYPE));
  httpPost.setEntity(baEntity);

  return httpPost;
 }

 private HttpGet buildGet(String url) 
   throws UnsupportedEncodingException {

  HttpGet httpGet = new HttpGet(url);

  return httpGet;
 }

 private HttpPut buildPut(String url, String content) 
   throws UnsupportedEncodingException {

  HttpPut httpPut = new HttpPut(url);
  ByteArrayEntity baEntity = new ByteArrayEntity(content.getBytes(UTF8));
  baEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, CONTENT_TYPE));
  httpPut.setEntity(baEntity);

  return httpPut;
 }

 private HttpDelete buildDelete(String url) 
   throws UnsupportedEncodingException {

  HttpDelete httpDelete = new HttpDelete(url);

  return httpDelete;
 }
}
And when i need more advanced features such as authentication I either extend or make the necessary changes. 
Hope this helps someone else.

05 December, 2013

Stock Me Up! by Coding Buffalo

These last couple of months I've been working with a friend on a new app for managing stock portfolios which we're publishing today in beta form.

The app is called Stock Me Up! and it just reached a point where it's stable and feature rich enough for everyone to use. The beta release is aimed at getting feedback from users to improve the app until it's everything it can be.
Here are some screenshots of the app as it is in version 1.0:




For this project my friend +Daniel Cachapa  and I formed a team called +Coding Buffalo aimed and getting solutions such as this app to the market. I'll keep you posted on new projects.
Anyway, if you're interested in financial stuff or are simply curious to see the app, you can download it here: https://play.google.com/store/apps/details?id=com.codingbuffalo.stockmeup.
The app itself is free with advertising. If you really hate ads there's a plugin to disable them. Please let us know what you think.

For more information visit: http://stockmeup.codingbuffalo.com