public class HttpClient extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
HttpClient.ExpectEmptyCallback
Version of
HttpClient.HttpCallback which expects the HTTP response body to be empty. |
static class |
HttpClient.HttpCallback
Wrapper around OkHttp's
Callback interface, specifically targeted at
expecting and handling JSON response bodies. |
| Constructor and Description |
|---|
HttpClient(String hostname) |
| Modifier and Type | Method and Description |
|---|---|
void |
clearBasicAuth()
Clears any previously-set basic authentication for HTTP requests.
|
void |
delete(String url,
HttpClient.HttpCallback callback)
Asynchronously perform an HTTP DELETE request on the given path.
|
Response |
deleteSynchronous(String url)
Synchronous version of
delete(String, HttpCallback). |
void |
get(String url,
HttpClient.HttpCallback callback)
Asynchronously perform an HTTP GET request on the given path.
|
URL |
getAbsoluteUrl(String relativePath)
Given a relative path (such as
"config"), return a URL object representing
the corresponding full path in the WVA's web services (such as
"http://192.168.100.1/ws/config"). |
Response |
getSynchronous(String url)
Synchronous version of
get(String, HttpCallback). |
protected RequestBody |
makeBody(JSONObject obj)
Converts a JSONObject, to be used as the body of a request, to its corresponding
RequestBody representation. |
protected Request.Builder |
makeBuilder(String url)
Create a new
Request.Builder for accessing the given URL, and adds the
Accept: application/json header, as well as an Authorization header if
authentication is being used. |
void |
post(String url,
JSONObject obj,
HttpClient.HttpCallback callback)
Asynchronously perform an HTTP POST request on the given path,
with the given JSON data.
|
Response |
postSynchronous(String url,
JSONObject obj)
Synchronous version of
put(String, JSONObject, HttpCallback). |
void |
put(String url,
JSONObject obj,
HttpClient.HttpCallback callback)
Asynchronously perform an HTTP PUT request on the given path,
with the given JSON data.
|
Response |
putSynchronous(String url,
JSONObject obj)
Synchronous version of
put(String, JSONObject, HttpCallback). |
void |
setHttpPort(int port)
Sets the port to be used when making HTTP requests.
|
void |
setHttpsPort(int port)
Sets the port to be used when making HTTPS requests.
|
void |
useBasicAuth(String username,
String password)
Sets the basic authentication parameters to be added to every HTTP request made by this client.
|
void |
useSecureHttp(boolean secure)
Sets whether this HTTP client should communicate over HTTP or HTTPS.
|
Callback |
wrapCallback(HttpClient.HttpCallback callback)
Wrap an HttpCallback in an OkHttp
Callback instance. |
public HttpClient(String hostname)
protected Request.Builder makeBuilder(String url)
Request.Builder for accessing the given URL, and adds the
Accept: application/json header, as well as an Authorization header if
authentication is being used.
This method is protected, rather than private, due to a bug between JaCoCo and the Android build tools which causes the instrumented bytecode to be invalid when this method is private: http://stackoverflow.com/questions/17603192/dalvik-transformation-using-wrong-invoke-opcode
url - the path, relative to /ws/, on which to perform a requestRequest.Builder objectprotected RequestBody makeBody(JSONObject obj)
RequestBody representation.
Used by post(java.lang.String, org.json.JSONObject, com.digi.wva.device.HttpClient.HttpCallback) and put(java.lang.String, org.json.JSONObject, com.digi.wva.device.HttpClient.HttpCallback), as well as their synchronous variants.
This method is protected, rather than private, due to a bug between JaCoCo and the Android build tools which causes the instrumented bytecode to be invalid when this method is private: http://stackoverflow.com/questions/17603192/dalvik-transformation-using-wrong-invoke-opcode
obj - the JSON data to be usedpublic Callback wrapCallback(HttpClient.HttpCallback callback)
Callback instance.
Also will automatically attempt to parse the response as JSON.
Used internally by HttpClient, and made public for unit-testing.
callback - the HttpClient.HttpCallback to wrapCallbackpublic void get(String url, HttpClient.HttpCallback callback)
url - the path, relative to /ws/, on which to perform a GET requestcallback - a callback for when the request completes or is in errorpublic Response getSynchronous(String url) throws IOException
get(String, HttpCallback). Invokes a GET
request to the given relative path immediately, and blocks until the response
can be processed or is in error.url - the path, relative to /ws/, on which to perform a GET requestResponse to the requestIOException - if the request could not be executed due to cancellation,
a connectivity problem or timeout. Because networks can fail
during an exchange, it is possible that the WVA accepted the
request before the failure.public void post(String url, JSONObject obj, HttpClient.HttpCallback callback)
url - the path, relative to /ws/, on which to perform a POST requestobj - the JSON object to POST to the devicecallback - a callback for when the request completes or is in errorpublic Response postSynchronous(String url, JSONObject obj) throws IOException
put(String, JSONObject, HttpCallback).
Invokes a POST request to the given relative path immediately, and blocks
until the response can be processed or is in error.url - the path, relative to /ws/, on which to perform a POST requestobj - the JSON object to POST to the deviceResponse to the requestIOException - if the request could not be executed due to cancellation,
a connectivity problem or timeout. Because networks can fail
during an exchange, it is possible that the WVA accepted the
request before the failure.public void put(String url, JSONObject obj, HttpClient.HttpCallback callback)
url - the path, relative to /ws/, on which to perform a PUT requestobj - the JSON object to PUT to the devicecallback - a callback for when the request completes or is in errorpublic Response putSynchronous(String url, JSONObject obj) throws IOException
put(String, JSONObject, HttpCallback).
Invokes a PUT request to the given relative path immediately, and blocks
until the response can be processed or is in error.url - the path, relative to /ws/, on which to perform a PUT requestobj - the JSON object to PUT to the deviceResponse to the requestIOException - if the request could not be executed due to cancellation,
a connectivity problem or timeout. Because networks can fail
during an exchange, it is possible that the WVA accepted the
request before the failure.public void delete(String url, HttpClient.HttpCallback callback)
url - the path, relative to /ws/, on which to perform a DELETE requestcallback - a callback for when the request completes or is in errorpublic Response deleteSynchronous(String url) throws IOException
delete(String, HttpCallback).
Invokes a DELETE request to the given relative path immediately, and blocks
until the response can be processed or is in error.url - the path, relative to /ws/, on which to perform a DELETE requestResponse to the requestIOException - if the request could not be executed due to cancellation,
a connectivity problem or timeout. Because networks can fail
during an exchange, it is possible that the WVA accepted the
request before the failure.public URL getAbsoluteUrl(String relativePath)
"config"), return a URL object representing
the corresponding full path in the WVA's web services (such as
"http://192.168.100.1/ws/config").
This method is used internally by HttpClient when constructing its HTTP requests.
relativePath - the web services path to usepublic void useBasicAuth(String username, String password)
username - the username for authenticationpassword - the password for authenticationpublic void clearBasicAuth()
public void useSecureHttp(boolean secure)
secure - true to use HTTPS, false to use HTTPpublic void setHttpPort(int port)
port - the port to use for HTTPpublic void setHttpsPort(int port)
port - the port to use for HTTPS