public class Response<T> extends Object
Modifier and Type | Method and Description |
---|---|
void |
ensureSuccess()
A shortcut to check for successful status codes and throw exception in case of non-2xx status codes.
|
T |
getBody()
Returns the payload of the response converted to the given type.
|
HttpURLConnection |
getConnection()
Get the "real" connection, typically to call some getters which are not provided by this Response object.
|
String |
getContentType()
Returns the MIME-type of the response body.
|
long |
getDate()
Returns the date when the request was created (server-time).
|
Object |
getErrorBody()
Get the body which was returned in case of error (HTTP-Code >= 400).
|
long |
getExpiration()
Returns the value of the expires header field.
|
String |
getHeaderField(String name)
Returns the value of the named header field.
|
long |
getHeaderFieldDate(String field,
long defaultValue)
Returns the value of the named field parsed as date (Millis since 1970).
|
int |
getHeaderFieldInt(String field,
int defaultValue)
Returns the value of the named field parsed as a number.
|
long |
getLastModified()
Returns the value of the last-modified header field.
|
Request |
getRequest()
Access to the
Request object (which will not be very useful in most cases). |
String |
getResponseMessage()
Returns the text explaining the status code.
|
int |
getStatusCode()
|
String |
getStatusLine()
The first line returned by the web-server, like "HTTP/1.1 200 OK".
|
boolean |
isSuccess()
Was the request successful (returning a 2xx status code)?
|
public Request getRequest()
Request
object (which will not be very useful in most cases).public T getBody()
public Object getErrorBody()
Content-Type
header (overrules the expected return type of the response)
asXyz()
). We try to coerce the error body to this type.
In case of REST services, where often a JSONObject is the normal response body, the error body
will be converted to JSONObject if possible. JSONArray
is not expected to be the
error body.
String
and byte[]
is used as
a fallback. You have to check the type with instanceof
or try/catch the cast.null
if there is no body or
no error.public int getStatusCode()
public String getStatusLine()
public boolean isSuccess()
true
when status code is between 200 and 299, else false
public String getResponseMessage()
public String getContentType()
public long getDate()
0
if this header was not set.public long getExpiration()
public long getLastModified()
public String getHeaderField(String name)
name
- name of the header fieldpublic long getHeaderFieldDate(String field, long defaultValue)
field
- name of the header fielddefaultValue
- the default value if the field is not present or malformedpublic int getHeaderFieldInt(String field, int defaultValue)
field
- name of the header fielddefaultValue
- the default value if the field is not present or malformedpublic HttpURLConnection getConnection()
public void ensureSuccess()
Request.ensureSuccess()
instead of this method.
But there might be cases where you want to inspect the response-object first (check header values) and
then have a short exit where the response-code is not suitable for further normal processing.Copyright © 2016. All rights reserved.