Android Open Source - mha-android Http Base






From Project

Back to project page mha-android.

License

The source code is released under:

Copyright (c) 2011-2012 Cameron Porter, Ryan Brown http://github.com/camporter/mha-android Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated...

If you think the Android project mha-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 com.teamacra.myhomeaudio.http;
//w w  w . j av  a 2s  .  c o  m
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import com.teamacra.myhomeaudio.MHAApplication;

import android.content.SharedPreferences;

/**
 * Base class for all other HTTP action classes.
 * 
 * @author Cameron
 * 
 */
public class HttpBase {

  protected MHAApplication app;
  
  protected HttpClient httpClient;
  protected HttpParams httpParams;

  public HttpBase(MHAApplication app) {
    this.app = app;
    
    this.httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 3000); // Default timeout is 5 seconds
    HttpConnectionParams.setSoTimeout(httpParams, 7000); // Default socket timeout is 10 seconds
    
    this.httpClient = new DefaultHttpClient(httpParams);
  }

  /**
   * Executes a request to the server that returns the response object.
   * 
   * @param apiUrl
   *            The portion of the URL indicating which part of the server's
   *            API to send to.
   * @param jsonRequestData
   *            The data to send to the server in JSON form.
   * @return The response in JSON form. Null if we don't get an object or something
   *         bad happens.
   */
  protected JSONObject executePostRequest(String apiUrl, JSONObject jsonRequestData) {
    try {
      HttpPost httpPost = new HttpPost("http://" + app.getServerAddress() + ":" + String.valueOf(app.getPort()) + apiUrl);
      
      httpPost.setEntity(new StringEntity(jsonRequestData.toString()));
      ResponseHandler<String> responseHandler = new BasicResponseHandler();
      try {
        String response = this.httpClient.execute(httpPost, responseHandler);
        JSONObject responseObject = new JSONObject(response);
        return responseObject;
      } catch (ClientProtocolException e) {
        // TODO: Do something with these possibilities later
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } catch (JSONException e) {
        e.printStackTrace();
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return null;
  }
  
  /**
   * Executes a request to the server that has a simple single-status
   * response.
   * 
   * @param apiUrl
   *            The portion of the URL indicating which part of the server's
   *            API to send to.
   * @param jsonRequestData
   *            The data to send to the server in JSON form.
   * @return The status code returned from the server.
   */
  protected int executeSimplePostRequest(String apiUrl, JSONObject jsonRequestData) {
    JSONObject responseObject = executePostRequest(apiUrl, jsonRequestData);
    if (responseObject != null) {
      try {
        return responseObject.getInt("status");
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    return StatusCode.STATUS_FAILED;
  }
}




Java Source Code List

com.teamacra.myhomeaudio.MHAApplication.java
com.teamacra.myhomeaudio.bluetooth.BluetoothService.java
com.teamacra.myhomeaudio.discovery.DiscoveryConstants.java
com.teamacra.myhomeaudio.discovery.DiscoveryDescription.java
com.teamacra.myhomeaudio.discovery.DiscoverySearchListener.java
com.teamacra.myhomeaudio.discovery.DiscoverySearch.java
com.teamacra.myhomeaudio.discovery.MDNSDiscovery.java
com.teamacra.myhomeaudio.http.HttpBase.java
com.teamacra.myhomeaudio.http.HttpClient.java
com.teamacra.myhomeaudio.http.HttpNode.java
com.teamacra.myhomeaudio.http.HttpSource.java
com.teamacra.myhomeaudio.http.HttpStream.java
com.teamacra.myhomeaudio.http.StatusCode.java
com.teamacra.myhomeaudio.locations.NodeSignalRange.java
com.teamacra.myhomeaudio.locations.NodeSignature.java
com.teamacra.myhomeaudio.manager.ConfigurationManager.java
com.teamacra.myhomeaudio.manager.LocationManager.java
com.teamacra.myhomeaudio.manager.NodeManager.java
com.teamacra.myhomeaudio.manager.StreamManager.java
com.teamacra.myhomeaudio.media.MediaDescriptor.java
com.teamacra.myhomeaudio.node.Node.java
com.teamacra.myhomeaudio.source.Source.java
com.teamacra.myhomeaudio.stream.StreamAction.java
com.teamacra.myhomeaudio.stream.Stream.java
com.teamacra.myhomeaudio.ui.InitialConfigActivity.java
com.teamacra.myhomeaudio.ui.LoginActivity.java
com.teamacra.myhomeaudio.ui.MyHomeAudioActivity.java
com.teamacra.myhomeaudio.ui.RegisterActivity.java
com.teamacra.myhomeaudio.ui.fragment.SongFragment.java
com.teamacra.myhomeaudio.ui.fragment.SourceFragment.java
com.teamacra.myhomeaudio.ui.fragment.TestFragment.java