Back to project page mha-android.
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.
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; } }