Class HttpService

java.lang.Object
com.brightcove.player.network.HttpService

public class HttpService extends Object
HttpService provides the basic tools to construct URLs properly, and issue HTTP requests, especially those that yield a JSON response.
  • Field Details

    • TAG

      public static final String TAG
    • DEFAULT_CONNECT_TIMEOUT

      public static final int DEFAULT_CONNECT_TIMEOUT
      Default connection timeout used by HttpService if none is given
      See Also:
    • DEFAULT_READ_TIMEOUT

      public static final int DEFAULT_READ_TIMEOUT
      Default read timeout used by HttpService if none is given
      See Also:
  • Constructor Details

    • HttpService

      public HttpService()
      HttpService When using the default constructor, DEFAULT_CONNECT_TIMEOUT and DEFAULT_READ_TIMEOUT are used for the timeout values.
    • HttpService

      public HttpService(int connectTimeout, int readTimeout)
      HttpService Provides overrides for the connection and read timeout values on instantiation. Please note: Setting override values to less than the DEFAULT_CONNECT_TIMEOUT and DEFAULT_READ_TIMEOUT values can result in unexpected request timeouts. Setting override values to less than 1 will force the use of DEFAULT_CONNECT_TIMEOUT and DEFAULT_READ_TIMEOUT as timeout values.
      Parameters:
      connectTimeout - The connection timeout for the underlying HttpURLConnection, in milliseconds. The default value is 10000 milliseconds.
      readTimeout - The read timeout for the underlying HttpURLConnection, in milliseconds. The default value is 10000 milliseconds
      See Also:
  • Method Details

    • getConnectTimeout

      public int getConnectTimeout()
      Retrieves the connection timeout used by this HttpService.
      Returns:
      the connection timeout in milliseconds
    • getReadTimeout

      public int getReadTimeout()
      Retrieves the read timeout used by this HttpService.
      Returns:
      the read timeout in milliseconds
    • buildURIWithQueryParameters

      public static URI buildURIWithQueryParameters(String baseUri, Map<String,Object> parameters) throws URISyntaxException, UnsupportedEncodingException
      Convenience method to construct a URL object from a base URL string and a Map of query string parameters.
      Parameters:
      baseUri - the scheme, hostname, and path portions of the URI to construct
      parameters - the query string parameters to be included in the URI (will be URI encoded)
      Returns:
      the fully populated URI
      Throws:
      URISyntaxException - if the base URI is malformed
      UnsupportedEncodingException
    • doGetRequest

      public String doGetRequest(URI uri) throws IOException
      Performs an HTTP GET request using the given URI.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP GET request
      Throws:
      IOException
    • runGetRequest

      public HttpResponse runGetRequest(URI uri) throws IOException
      This is the same as {doGetRequest(URI)} but returns a {{HttpResponse} instead.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP GET request
      Throws:
      IOException
    • doGetRequest

      public String doGetRequest(URI uri, Map<String,String> headers) throws IOException
      Performs an HTTP GET request using the given URI.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP GET request
      Throws:
      IOException
    • runGetRequest

      public HttpResponse runGetRequest(URI uri, Map<String,String> headers) throws IOException
      This is the same as {doGetRequest(URI, Map)} but returns a {{HttpResponse} instead.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP GET request
      Throws:
      IOException
    • doPostRequest

      public String doPostRequest(URI uri) throws IOException
      Performs an HTTP POST request using the given URI.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP POST request
      Throws:
      IOException
    • runPostRequest

      public HttpResponse runPostRequest(URI uri, Map<String,String> headers) throws IOException
      This is the same as {doPostRequest(URI)} but returns a {{HttpResponse} instead.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP POST request
      Throws:
      IOException
    • doPostRequest

      public String doPostRequest(URI uri, Map<String,String> headers) throws IOException
      Performs an HTTP POST request using the given URI.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP POST request
      Throws:
      IOException
    • doDeleteRequest

      public String doDeleteRequest(URI uri, Map<String,String> headers) throws IOException
      Performs an HTTP PUT request using the given URI.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP PUT request
      Throws:
      IOException
    • runDeleteRequest

      public HttpResponse runDeleteRequest(URI uri, Map<String,String> headers) throws IOException
      This is the same as {doDeleteRequest(URI, Map)} but returns a {{HttpResponse} instead.
      Parameters:
      uri - the request URI
      Returns:
      a String containing the response body of the HTTP PUT request
      Throws:
      IOException
    • doImageGetRequest

      public android.graphics.Bitmap doImageGetRequest(URI uri) throws IOException
      Performs and HTTP GET request expecting a bitmap image file in the response.
      Parameters:
      uri - the request URI
      Returns:
      a Bitmap decoded from the response content stream
      Throws:
      IOException
    • doImageGetRequest

      public android.graphics.Bitmap doImageGetRequest(URI uri, int maxWidth, int maxHeight) throws IOException
      Performs and HTTP GET request expecting a bitmap image file in the response.
      Parameters:
      uri - the request URI
      maxWidth - The maximum width of the target view.
      maxHeight - The maximum height of the target view.
      Returns:
      a Bitmap decoded from the response content stream
      Throws:
      IOException
    • doJSONGetRequest

      public org.json.JSONObject doJSONGetRequest(URI uri) throws IOException, org.json.JSONException
      Performs an HTTP GET request assuming that the response body will contain a valid JSON document. The JSON document is parsed and returned as a JSONObject.
      Parameters:
      uri - the request URI
      Returns:
      a JSONObject created by parsing the HTTP response, or null if the response is empty
      Throws:
      IOException
      org.json.JSONException
    • doJSONGetRequest

      public org.json.JSONObject doJSONGetRequest(URI uri, Map<String,String> headers) throws IOException, org.json.JSONException
      Performs an HTTP GET request assuming that the response body will contain a valid JSON document. The JSON document is parsed and returned as a JSONObject.
      Parameters:
      uri - the request URI
      headers - the request headers
      Returns:
      a JSONObject created by parsing the HTTP response, or null if the response is empty
      Throws:
      IOException
      org.json.JSONException
    • doJSONPostRequest

      public org.json.JSONObject doJSONPostRequest(URI uri) throws IOException, org.json.JSONException
      Performs an HTTP POST request assuming that the response body will contain a valid JSON document. The JSON document is parsed and returned as a JSONObject.
      Parameters:
      uri - the request URI
      Returns:
      a JSONObject created by parsing the HTTP response, or null if the response is empty
      Throws:
      IOException
      org.json.JSONException
    • doJSONPostRequest

      public org.json.JSONObject doJSONPostRequest(URI uri, Map<String,String> headers) throws IOException, org.json.JSONException
      Performs an HTTP POST request assuming that the response body will contain a valid JSON document. The JSON document is parsed and returned as a JSONObject.
      Parameters:
      uri - the request URI
      headers - the request headers
      Returns:
      a JSONObject created by parsing the HTTP response, or null if the response is empty
      Throws:
      IOException
      org.json.JSONException
    • doJSONPostRequest

      public org.json.JSONObject doJSONPostRequest(URI uri, Map<String,String> headers, String requestBody) throws IOException, org.json.JSONException
      Throws:
      IOException
      org.json.JSONException
    • runJSONGetRequest

      public HttpResponse runJSONGetRequest(URI uri) throws IOException, org.json.JSONException
      Same as {doJSONGetRequest(URI)} but this returns a {{HttpResponse}} instead.
      Parameters:
      uri - the request URI
      Returns:
      a JSONObject created by parsing the HTTP response, or null if the response is empty
      Throws:
      IOException
      org.json.JSONException
    • runJSONGetRequest

      public HttpResponse runJSONGetRequest(URI uri, Map<String,String> headers) throws IOException, org.json.JSONException
      Same as {doJSONGetRequest(URI, Map) )}} but this returns a {{HttpResponse}} instead.
      Parameters:
      uri - the request URI
      headers - the request headers
      Returns:
      a JSONObject created by parsing the HTTP response, or null if the response is empty
      Throws:
      IOException
      org.json.JSONException
    • runJSONPostRequest

      public HttpResponse runJSONPostRequest(URI uri) throws IOException, org.json.JSONException
      Same as {doJSONPostRequest(URI)} but this returns a {{HttpResponse}} instead.
      Parameters:
      uri - the request URI
      Returns:
      a JSONObject created by parsing the HTTP response, or null if the response is empty
      Throws:
      IOException
      org.json.JSONException
    • runJSONPostRequest

      public HttpResponse runJSONPostRequest(URI uri, Map<String,String> headers) throws IOException, org.json.JSONException
      Same as {doJSONPostRequest(URI, Map)} but this returns a {{HttpResponse}} instead.
      Parameters:
      uri - the request URI
      headers - the request headers
      Returns:
      a JSONObject created by parsing the HTTP response, or null if the response is empty
      Throws:
      IOException
      org.json.JSONException
    • runJSONPostRequest

      public HttpResponse runJSONPostRequest(URI uri, Map<String,String> headers, String requestBody) throws IOException, org.json.JSONException
      Throws:
      IOException
      org.json.JSONException
    • inputStreamToString

      public static String inputStreamToString(InputStream inputStream) throws IOException
      Reads all data from the given InputStream and converts to a String (Uses StringBuilder).
      Parameters:
      inputStream - InputStream from which String should be built
      Returns:
      a String representation of the data provided by the InputStream
      Throws:
      IOException
    • readerToString

      public static String readerToString(Reader reader) throws IOException
      Reads all data from the given Reader and converts to a String (Uses StringBuilder).
      Parameters:
      reader - Reader from which String should be built
      Returns:
      a String representation of the data provided by the Reader
      Throws:
      IOException
    • parseToJSONObject

      public static org.json.JSONObject parseToJSONObject(String jsonString) throws org.json.JSONException
      Parses given string containing JSON data into a JSONObject which provides an interface for extracting JSON data. Note that this method only supports parsing JSON strings that are represented by a top-level object, i.e., starts with a curly brace. Top level arrays are not supported by this method (i.e., starts with square bracket).
      Parameters:
      jsonString - the JSON object data to be parsed (must not be null or empty)
      Returns:
      the parsed JSONObject
      Throws:
      org.json.JSONException - if there are any issues parsing the JSON