Package com.brightcove.player.network
Class HttpService
java.lang.Object
com.brightcove.player.network.HttpService
HttpService provides the basic tools to construct URLs properly, and issue HTTP requests,
especially those that yield a JSON response.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Default connection timeout used by HttpService if none is givenstatic final int
Default read timeout used by HttpService if none is givenstatic final String
-
Constructor Summary
ConstructorDescriptionHttpService When using the default constructor, DEFAULT_CONNECT_TIMEOUT and DEFAULT_READ_TIMEOUT are used for the timeout values.HttpService
(int connectTimeout, int readTimeout) HttpService Provides overrides for the connection and read timeout values on instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic URI
buildURIWithQueryParameters
(String baseUri, Map<String, Object> parameters) Convenience method to construct a URL object from a base URL string and a Map of query string parameters.doDeleteRequest
(URI uri, Map<String, String> headers) Performs an HTTP PUT request using the given URI.doGetRequest
(URI uri) Performs an HTTP GET request using the given URI.doGetRequest
(URI uri, Map<String, String> headers) Performs an HTTP GET request using the given URI.android.graphics.Bitmap
doImageGetRequest
(URI uri) Performs and HTTP GET request expecting a bitmap image file in the response.android.graphics.Bitmap
doImageGetRequest
(URI uri, int maxWidth, int maxHeight) Performs and HTTP GET request expecting a bitmap image file in the response.org.json.JSONObject
doJSONGetRequest
(URI uri) Performs an HTTP GET request assuming that the response body will contain a valid JSON document.org.json.JSONObject
doJSONGetRequest
(URI uri, Map<String, String> headers) Performs an HTTP GET request assuming that the response body will contain a valid JSON document.org.json.JSONObject
doJSONPostRequest
(URI uri) Performs an HTTP POST request assuming that the response body will contain a valid JSON document.org.json.JSONObject
doJSONPostRequest
(URI uri, Map<String, String> headers) Performs an HTTP POST request assuming that the response body will contain a valid JSON document.org.json.JSONObject
doPostRequest
(URI uri) Performs an HTTP POST request using the given URI.doPostRequest
(URI uri, Map<String, String> headers) Performs an HTTP POST request using the given URI.int
Retrieves the connection timeout used by this HttpService.int
Retrieves the read timeout used by this HttpService.static String
inputStreamToString
(InputStream inputStream) Reads all data from the given InputStream and converts to a String (Uses StringBuilder).static org.json.JSONObject
parseToJSONObject
(String jsonString) Parses given string containing JSON data into a JSONObject which provides an interface for extracting JSON data.static String
readerToString
(Reader reader) Reads all data from the given Reader and converts to a String (Uses StringBuilder).runDeleteRequest
(URI uri, Map<String, String> headers) This is the same as {doDeleteRequest(URI, Map)
} but returns a {{HttpResponse
} instead.runGetRequest
(URI uri) This is the same as {doGetRequest(URI)
} but returns a {{HttpResponse
} instead.runGetRequest
(URI uri, Map<String, String> headers) This is the same as {doGetRequest(URI, Map)
} but returns a {{HttpResponse
} instead.runJSONGetRequest
(URI uri) Same as {doJSONGetRequest(URI)
} but this returns a {{HttpResponse
}} instead.runJSONGetRequest
(URI uri, Map<String, String> headers) Same as {doJSONGetRequest(URI, Map)
)}} but this returns a {{HttpResponse
}} instead.runJSONPostRequest
(URI uri) Same as {doJSONPostRequest(URI)
} but this returns a {{HttpResponse
}} instead.runJSONPostRequest
(URI uri, Map<String, String> headers) Same as {doJSONPostRequest(URI, Map)
} but this returns a {{HttpResponse
}} instead.runPostRequest
(URI uri, Map<String, String> headers) This is the same as {doPostRequest(URI)
} but returns a {{HttpResponse
} instead.
-
Field Details
-
TAG
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUTDefault connection timeout used by HttpService if none is given- See Also:
-
DEFAULT_READ_TIMEOUT
public static final int DEFAULT_READ_TIMEOUTDefault 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, UnsupportedEncodingExceptionConvenience 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 constructparameters
- 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 malformedUnsupportedEncodingException
-
doGetRequest
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
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
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
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
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
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
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
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
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
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 URImaxWidth
- 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
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.JSONExceptionPerforms 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 URIheaders
- 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
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.JSONExceptionPerforms 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 URIheaders
- 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
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.JSONExceptionSame as {doJSONGetRequest(URI, Map)
)}} but this returns a {{HttpResponse
}} instead.- Parameters:
uri
- the request URIheaders
- 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
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.JSONExceptionSame as {doJSONPostRequest(URI, Map)
} but this returns a {{HttpResponse
}} instead.- Parameters:
uri
- the request URIheaders
- 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
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
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
-