Android Open Source - mha-android Http Client






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;
//from   w  w w  .  ja  va  2s .c  o  m
import java.util.Arrays;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.teamacra.myhomeaudio.MHAApplication;
import com.teamacra.myhomeaudio.stream.Stream;

public class HttpClient extends HttpBase {

  public HttpClient(MHAApplication app) {
    super(app);
  }

  /**
   * Sends a login request to the server using the information given.
   * 
   * @param username
   * @param password
   * @return First element - The sessionID for the user, Second element - The
   *         configured status. Returns an array with each element set to null
   *         if the login failed.
   */
  public String[] login(String username, String password) {
    JSONObject requestObject = new JSONObject();
    String[] responseData = { null, null };
    try {
      requestObject.put("username", username);
      requestObject.put("password", password);
      requestObject.put("ipaddress", app.getLocalAddress());
      requestObject.put("macaddress", app.getMacAddress());
      requestObject.put("bluetoothname", app.getBluetoothName());
      JSONObject responseObject = executePostRequest("/client/login",
          requestObject);
      if (responseObject != null
          && responseObject.getInt("status") == StatusCode.STATUS_OK) {
        responseData[0] = responseObject.getString("session");
        responseData[1] = responseObject.getString("configured");
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
    System.out.println("Response:" + Arrays.toString(responseData));
    return responseData;
  }

  /**
   * Sends a user register request to the server using the given information.
   * 
   * @param username
   * @param password
   * @return The status code from the server indicating the result.
   */
  public int register(String username, String password) {
    JSONObject requestObject = new JSONObject();
    try {
      requestObject.put("username", username);
      requestObject.put("password", password);
      return executeSimplePostRequest("/user/register", requestObject);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return StatusCode.STATUS_FAILED;
  }

  /**
   * Sends a user logout request to the server using the given information.
   * 
   * @param sessionId
   *            The session to log out of.
   * @return The status code from the server indicating the result.
   */
  public int logout() {
    JSONObject requestObject = new JSONObject();
    try {
      requestObject.put("session", app.getSessionId());
      return executeSimplePostRequest("/client/logout", requestObject);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return StatusCode.STATUS_FAILED;
  }

  public int initialConfig(JSONArray signaturesAsJSON) {
    JSONObject requestObject = new JSONObject();
    try {
      requestObject.put("session", app.getSessionId());
      requestObject.put("signatures", signaturesAsJSON);
      return executeSimplePostRequest("/client/initialconfig",
          requestObject);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return StatusCode.STATUS_FAILED;
  }

  public int location(JSONArray devices, Stream stream){
    JSONObject requestObject = new JSONObject();
    try{
      requestObject.put("session", app.getSessionId());
      requestObject.put("locations",devices);
      requestObject.put("stream", stream.id());
      return executeSimplePostRequest("/client/locations",requestObject);
    }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