-
public class HttpService
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 public final static String
TAG
public final static int
DEFAULT_CONNECT_TIMEOUT
public final static int
DEFAULT_READ_TIMEOUT
private int
connectTimeout
private int
readTimeout
-
Constructor Summary
Constructors Constructor Description HttpService()
HttpServiceWhen using the default constructor, DEFAULT_CONNECT_TIMEOUT and DEFAULT_READ_TIMEOUT areused for the timeout values. HttpService(int connectTimeout, int readTimeout)
HttpServiceProvides 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.
-
Method Summary
Modifier and Type Method Description int
getConnectTimeout()
Retrieves the connection timeout used by this HttpService. void
setConnectTimeout(int connectTimeoutMs)
Sets the connection timeout to be used by this HttpService int
getReadTimeout()
Retrieves the read timeout used by this HttpService. void
setReadTimeout(int readTimeoutMs)
Sets the read timeout to be used by this HttpService static URI
buildURIWithQueryParameters(String baseUri, Map<String, Object> parameters)
Convenience method to construct a URL object from a base URL string and a Map of querystring parameters. String
doGetRequest(URI uri)
Performs an HTTP GET request using the given URI. HttpResponse
runGetRequest(URI uri)
This is the same as {doGetRequest} but returnsa {{HttpResponse} instead. String
doGetRequest(URI uri, Map<String, String> headers)
Performs an HTTP GET request using the given URI. HttpResponse
runGetRequest(URI uri, Map<String, String> headers)
This is the same as {doGetRequest} but returnsa {{HttpResponse} instead. String
doPostRequest(URI uri)
Performs an HTTP POST request using the given URI. HttpResponse
runPostRequest(URI uri, Map<String, String> headers)
This is the same as {doPostRequest} but returnsa {{HttpResponse} instead. String
doPostRequest(URI uri, Map<String, String> headers)
Performs an HTTP POST request using the given URI. String
doDeleteRequest(URI uri, Map<String, String> headers)
Performs an HTTP PUT request using the given URI. HttpResponse
runDeleteRequest(URI uri, Map<String, String> headers)
This is the same as {doDeleteRequest} but returnsa {{HttpResponse} instead. Bitmap
doImageGetRequest(URI uri)
Performs and HTTP GET request expecting a bitmap image file in the response. Bitmap
doImageGetRequest(URI uri, int maxWidth, int maxHeight)
Performs and HTTP GET request expecting a bitmap image file in the response. JSONObject
doJSONGetRequest(URI uri)
Performs an HTTP GET request assuming that the response body will contain a valid JSONdocument. JSONObject
doJSONGetRequest(URI uri, Map<String, String> headers)
Performs an HTTP GET request assuming that the response body will contain a valid JSONdocument. JSONObject
doJSONPostRequest(URI uri)
Performs an HTTP POST request assuming that the response body will contain a valid JSONdocument. JSONObject
doJSONPostRequest(URI uri, Map<String, String> headers)
Performs an HTTP POST request assuming that the response body will contain a valid JSONdocument. JSONObject
doJSONPostRequest(URI uri, Map<String, String> headers, String requestBody)
HttpResponse
runJSONGetRequest(URI uri)
Same as {doJSONGetRequest} but this returns a {{HttpResponse}} instead. HttpResponse
runJSONGetRequest(URI uri, Map<String, String> headers)
Same as {doJSONGetRequest )}} but this returns a {{HttpResponse}} instead. HttpResponse
runJSONPostRequest(URI uri)
Same as {doJSONPostRequest} but this returns a {{HttpResponse}} instead. HttpResponse
runJSONPostRequest(URI uri, Map<String, String> headers)
Same as {doJSONPostRequest} but this returns a {{HttpResponse}} instead. HttpResponse
runJSONPostRequest(URI uri, Map<String, String> headers, String requestBody)
static String
inputStreamToString(InputStream inputStream)
Reads all data from the given InputStream and converts to a String (Uses StringBuilder). static String
readerToString(Reader reader)
Reads all data from the given Reader and converts to a String (Uses StringBuilder). static JSONObject
parseToJSONObject(String jsonString)
Parses given string containing JSON data into a JSONObject which provides an interfacefor extracting JSON data. -
-
Constructor Detail
-
HttpService
HttpService()
HttpServiceWhen using the default constructor, DEFAULT_CONNECT_TIMEOUT and DEFAULT_READ_TIMEOUT areused for the timeout values.
-
HttpService
HttpService(int connectTimeout, int readTimeout)
HttpServiceProvides 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.readTimeout
- The read timeout for the underlying HttpURLConnection, in milliseconds.
-
-
Method Detail
-
getConnectTimeout
int getConnectTimeout()
Retrieves the connection timeout used by this HttpService.
-
setConnectTimeout
void setConnectTimeout(int connectTimeoutMs)
Sets the connection timeout to be used by this HttpService
- Parameters:
connectTimeoutMs
- the value in millis
-
getReadTimeout
int getReadTimeout()
Retrieves the read timeout used by this HttpService.
-
setReadTimeout
void setReadTimeout(int readTimeoutMs)
Sets the read timeout to be used by this HttpService
- Parameters:
readTimeoutMs
- the value in millis
-
buildURIWithQueryParameters
static URI buildURIWithQueryParameters(String baseUri, Map<String, Object> parameters)
Convenience method to construct a URL object from a base URL string and a Map of querystring 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)
-
doGetRequest
String doGetRequest(URI uri)
Performs an HTTP GET request using the given URI.
- Parameters:
uri
- the request URI
-
runGetRequest
HttpResponse runGetRequest(URI uri)
This is the same as {doGetRequest} but returnsa {{HttpResponse} instead.
- Parameters:
uri
- the request URI
-
doGetRequest
String doGetRequest(URI uri, Map<String, String> headers)
Performs an HTTP GET request using the given URI.
- Parameters:
uri
- the request URI
-
runGetRequest
HttpResponse runGetRequest(URI uri, Map<String, String> headers)
This is the same as {doGetRequest} but returnsa {{HttpResponse} instead.
- Parameters:
uri
- the request URI
-
doPostRequest
String doPostRequest(URI uri)
Performs an HTTP POST request using the given URI.
- Parameters:
uri
- the request URI
-
runPostRequest
HttpResponse runPostRequest(URI uri, Map<String, String> headers)
This is the same as {doPostRequest} but returnsa {{HttpResponse} instead.
- Parameters:
uri
- the request URI
-
doPostRequest
String doPostRequest(URI uri, Map<String, String> headers)
Performs an HTTP POST request using the given URI.
- Parameters:
uri
- the request URI
-
doDeleteRequest
String doDeleteRequest(URI uri, Map<String, String> headers)
Performs an HTTP PUT request using the given URI.
- Parameters:
uri
- the request URI
-
runDeleteRequest
HttpResponse runDeleteRequest(URI uri, Map<String, String> headers)
This is the same as {doDeleteRequest} but returnsa {{HttpResponse} instead.
- Parameters:
uri
- the request URI
-
doImageGetRequest
Bitmap doImageGetRequest(URI uri)
Performs and HTTP GET request expecting a bitmap image file in the response.
- Parameters:
uri
- the request URI
-
doImageGetRequest
Bitmap doImageGetRequest(URI uri, int maxWidth, int maxHeight)
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.
-
doJSONGetRequest
JSONObject doJSONGetRequest(URI uri)
Performs an HTTP GET request assuming that the response body will contain a valid JSONdocument. The JSON document is parsed and returned as a JSONObject.
- Parameters:
uri
- the request URI
-
doJSONGetRequest
JSONObject doJSONGetRequest(URI uri, Map<String, String> headers)
Performs an HTTP GET request assuming that the response body will contain a valid JSONdocument. The JSON document is parsed and returned as a JSONObject.
- Parameters:
uri
- the request URIheaders
- the request headers
-
doJSONPostRequest
JSONObject doJSONPostRequest(URI uri)
Performs an HTTP POST request assuming that the response body will contain a valid JSONdocument. The JSON document is parsed and returned as a JSONObject.
- Parameters:
uri
- the request URI
-
doJSONPostRequest
JSONObject doJSONPostRequest(URI uri, Map<String, String> headers)
Performs an HTTP POST request assuming that the response body will contain a valid JSONdocument. The JSON document is parsed and returned as a JSONObject.
- Parameters:
uri
- the request URIheaders
- the request headers
-
doJSONPostRequest
JSONObject doJSONPostRequest(URI uri, Map<String, String> headers, String requestBody)
-
runJSONGetRequest
HttpResponse runJSONGetRequest(URI uri)
Same as {doJSONGetRequest} but this returns a {{HttpResponse}} instead.
- Parameters:
uri
- the request URI
-
runJSONGetRequest
HttpResponse runJSONGetRequest(URI uri, Map<String, String> headers)
Same as {doJSONGetRequest )}} but this returns a {{HttpResponse}} instead.
- Parameters:
uri
- the request URIheaders
- the request headers
-
runJSONPostRequest
HttpResponse runJSONPostRequest(URI uri)
Same as {doJSONPostRequest} but this returns a {{HttpResponse}} instead.
- Parameters:
uri
- the request URI
-
runJSONPostRequest
HttpResponse runJSONPostRequest(URI uri, Map<String, String> headers)
Same as {doJSONPostRequest} but this returns a {{HttpResponse}} instead.
- Parameters:
uri
- the request URIheaders
- the request headers
-
runJSONPostRequest
HttpResponse runJSONPostRequest(URI uri, Map<String, String> headers, String requestBody)
-
inputStreamToString
static String inputStreamToString(InputStream inputStream)
Reads all data from the given InputStream and converts to a String (Uses StringBuilder).
- Parameters:
inputStream
- InputStream from which String should be built
-
readerToString
static String readerToString(Reader reader)
Reads all data from the given Reader and converts to a String (Uses StringBuilder).
- Parameters:
reader
- Reader from which String should be built
-
parseToJSONObject
static JSONObject parseToJSONObject(String jsonString)
Parses given string containing JSON data into a JSONObject which provides an interfacefor extracting JSON data. Note that this method only supports parsing JSON strings thatare represented by a top-level object, i.e., starts with a curly brace. Top level arraysare 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)
-
-
-
-