Package com.brightcove.player.network
Class HttpService
- java.lang.Object
-
- com.brightcove.player.network.HttpService
-
public class HttpService extends java.lang.Object
HttpService provides the basic tools to construct URLs properly, and issue HTTP requests, especially those that yield a JSON response.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_CONNECT_TIMEOUT
Default connection timeout used by HttpService if none is givenstatic int
DEFAULT_READ_TIMEOUT
Default read timeout used by HttpService if none is givenstatic java.lang.String
TAG
-
Constructor Summary
Constructors Constructor Description HttpService()
HttpService 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.net.URI
buildURIWithQueryParameters(java.lang.String baseUri, java.util.Map<java.lang.String,java.lang.Object> parameters)
Convenience method to construct a URL object from a base URL string and a Map of query string parameters.java.lang.String
doDeleteRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
Performs an HTTP PUT request using the given URI.java.lang.String
doGetRequest(java.net.URI uri)
Performs an HTTP GET request using the given URI.java.lang.String
doGetRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
Performs an HTTP GET request using the given URI.android.graphics.Bitmap
doImageGetRequest(java.net.URI uri)
Performs and HTTP GET request expecting a bitmap image file in the response.android.graphics.Bitmap
doImageGetRequest(java.net.URI uri, int maxWidth, int maxHeight)
Performs and HTTP GET request expecting a bitmap image file in the response.org.json.JSONObject
doJSONGetRequest(java.net.URI uri)
Performs an HTTP GET request assuming that the response body will contain a valid JSON document.org.json.JSONObject
doJSONGetRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
Performs an HTTP GET request assuming that the response body will contain a valid JSON document.org.json.JSONObject
doJSONPostRequest(java.net.URI uri)
Performs an HTTP POST request assuming that the response body will contain a valid JSON document.org.json.JSONObject
doJSONPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
Performs an HTTP POST request assuming that the response body will contain a valid JSON document.org.json.JSONObject
doJSONPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String requestBody)
java.lang.String
doPostRequest(java.net.URI uri)
Performs an HTTP POST request using the given URI.java.lang.String
doPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
Performs an HTTP POST request using the given URI.int
getConnectTimeout()
Retrieves the connection timeout used by this HttpService.int
getReadTimeout()
Retrieves the read timeout used by this HttpService.static java.lang.String
inputStreamToString(java.io.InputStream inputStream)
Reads all data from the given InputStream and converts to a String (Uses StringBuilder).static org.json.JSONObject
parseToJSONObject(java.lang.String jsonString)
Parses given string containing JSON data into a JSONObject which provides an interface for extracting JSON data.static java.lang.String
readerToString(java.io.Reader reader)
Reads all data from the given Reader and converts to a String (Uses StringBuilder).HttpResponse
runDeleteRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
This is the same as {doDeleteRequest(URI, Map)
} but returns a {{HttpResponse
} instead.HttpResponse
runGetRequest(java.net.URI uri)
This is the same as {doGetRequest(URI)
} but returns a {{HttpResponse
} instead.HttpResponse
runGetRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
This is the same as {doGetRequest(URI, Map)
} but returns a {{HttpResponse
} instead.HttpResponse
runJSONGetRequest(java.net.URI uri)
Same as {doJSONGetRequest(URI)
} but this returns a {{HttpResponse
}} instead.HttpResponse
runJSONGetRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
Same as {doJSONGetRequest(URI, Map)
)}} but this returns a {{HttpResponse
}} instead.HttpResponse
runJSONPostRequest(java.net.URI uri)
Same as {doJSONPostRequest(URI)
} but this returns a {{HttpResponse
}} instead.HttpResponse
runJSONPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
Same as {doJSONPostRequest(URI, Map)
} but this returns a {{HttpResponse
}} instead.HttpResponse
runJSONPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String requestBody)
HttpResponse
runPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers)
This is the same as {doPostRequest(URI)
} but returns a {{HttpResponse
} instead.
-
-
-
Field Detail
-
TAG
public static final java.lang.String TAG
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT
Default connection timeout used by HttpService if none is given- See Also:
- Constant Field Values
-
DEFAULT_READ_TIMEOUT
public static final int DEFAULT_READ_TIMEOUT
Default read timeout used by HttpService if none is given- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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:
HttpURLConnection
-
-
Method Detail
-
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 java.net.URI buildURIWithQueryParameters(java.lang.String baseUri, java.util.Map<java.lang.String,java.lang.Object> parameters) throws java.net.URISyntaxException, java.io.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 constructparameters
- the query string parameters to be included in the URI (will be URI encoded)- Returns:
- the fully populated URI
- Throws:
java.net.URISyntaxException
- if the base URI is malformedjava.io.UnsupportedEncodingException
-
doGetRequest
public java.lang.String doGetRequest(java.net.URI uri) throws java.io.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:
java.io.IOException
-
runGetRequest
public HttpResponse runGetRequest(java.net.URI uri) throws java.io.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:
java.io.IOException
-
doGetRequest
public java.lang.String doGetRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.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:
java.io.IOException
-
runGetRequest
public HttpResponse runGetRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.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:
java.io.IOException
-
doPostRequest
public java.lang.String doPostRequest(java.net.URI uri) throws java.io.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:
java.io.IOException
-
runPostRequest
public HttpResponse runPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.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:
java.io.IOException
-
doPostRequest
public java.lang.String doPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.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:
java.io.IOException
-
doDeleteRequest
public java.lang.String doDeleteRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.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:
java.io.IOException
-
runDeleteRequest
public HttpResponse runDeleteRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.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:
java.io.IOException
-
doImageGetRequest
public android.graphics.Bitmap doImageGetRequest(java.net.URI uri) throws java.io.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:
java.io.IOException
-
doImageGetRequest
public android.graphics.Bitmap doImageGetRequest(java.net.URI uri, int maxWidth, int maxHeight) throws java.io.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:
java.io.IOException
-
doJSONGetRequest
public org.json.JSONObject doJSONGetRequest(java.net.URI uri) throws java.io.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:
java.io.IOException
org.json.JSONException
-
doJSONGetRequest
public org.json.JSONObject doJSONGetRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.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 URIheaders
- the request headers- Returns:
- a JSONObject created by parsing the HTTP response, or null if the response is empty
- Throws:
java.io.IOException
org.json.JSONException
-
doJSONPostRequest
public org.json.JSONObject doJSONPostRequest(java.net.URI uri) throws java.io.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:
java.io.IOException
org.json.JSONException
-
doJSONPostRequest
public org.json.JSONObject doJSONPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.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 URIheaders
- the request headers- Returns:
- a JSONObject created by parsing the HTTP response, or null if the response is empty
- Throws:
java.io.IOException
org.json.JSONException
-
doJSONPostRequest
public org.json.JSONObject doJSONPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String requestBody) throws java.io.IOException, org.json.JSONException
- Throws:
java.io.IOException
org.json.JSONException
-
runJSONGetRequest
public HttpResponse runJSONGetRequest(java.net.URI uri) throws java.io.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:
java.io.IOException
org.json.JSONException
-
runJSONGetRequest
public HttpResponse runJSONGetRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.IOException, org.json.JSONException
Same 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:
java.io.IOException
org.json.JSONException
-
runJSONPostRequest
public HttpResponse runJSONPostRequest(java.net.URI uri) throws java.io.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:
java.io.IOException
org.json.JSONException
-
runJSONPostRequest
public HttpResponse runJSONPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers) throws java.io.IOException, org.json.JSONException
Same 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:
java.io.IOException
org.json.JSONException
-
runJSONPostRequest
public HttpResponse runJSONPostRequest(java.net.URI uri, java.util.Map<java.lang.String,java.lang.String> headers, java.lang.String requestBody) throws java.io.IOException, org.json.JSONException
- Throws:
java.io.IOException
org.json.JSONException
-
inputStreamToString
public static java.lang.String inputStreamToString(java.io.InputStream inputStream) throws java.io.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:
java.io.IOException
-
readerToString
public static java.lang.String readerToString(java.io.Reader reader) throws java.io.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:
java.io.IOException
-
parseToJSONObject
public static org.json.JSONObject parseToJSONObject(java.lang.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
-
-