public final class OkHttpURLConnection extends HttpURLConnection implements Callback
connected field from the
superclass. That field is not used to indicate whether this URLConnection is
currently connected. Instead, it indicates whether a connection has ever been attempted. Once a
connection has been attempted, certain properties (request header fields, request method, etc.)
are immutable.| Modifier and Type | Field and Description |
|---|---|
static String |
RESPONSE_SOURCE
Synthetic response header: the location from which the response was loaded.
|
static String |
SELECTED_PROTOCOL
Synthetic response header: the selected
protocol ("spdy/3.1", "http/1.1",
etc). |
chunkLength, fixedContentLengthLong, HTTP_ACCEPTED, HTTP_BAD_GATEWAY, HTTP_BAD_METHOD, HTTP_BAD_REQUEST, HTTP_CLIENT_TIMEOUT, HTTP_CONFLICT, HTTP_CREATED, HTTP_ENTITY_TOO_LARGE, HTTP_FORBIDDEN, HTTP_GATEWAY_TIMEOUT, HTTP_GONE, HTTP_INTERNAL_ERROR, HTTP_LENGTH_REQUIRED, HTTP_MOVED_PERM, HTTP_MOVED_TEMP, HTTP_MULT_CHOICE, HTTP_NO_CONTENT, HTTP_NOT_ACCEPTABLE, HTTP_NOT_AUTHORITATIVE, HTTP_NOT_FOUND, HTTP_NOT_IMPLEMENTED, HTTP_NOT_MODIFIED, HTTP_OK, HTTP_PARTIAL, HTTP_PAYMENT_REQUIRED, HTTP_PRECON_FAILED, HTTP_PROXY_AUTH, HTTP_REQ_TOO_LONG, HTTP_RESET, HTTP_SEE_OTHER, HTTP_SERVER_ERROR, HTTP_UNAUTHORIZED, HTTP_UNAVAILABLE, HTTP_UNSUPPORTED_TYPE, HTTP_USE_PROXY, HTTP_VERSION, instanceFollowRedirects, method, responseCode, responseMessageallowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches| Constructor and Description |
|---|
OkHttpURLConnection(URL url,
OkHttpClient client) |
OkHttpURLConnection(URL url,
OkHttpClient client,
URLFilter urlFilter) |
| Modifier and Type | Method and Description |
|---|---|
void |
addRequestProperty(String field,
String value) |
void |
connect() |
void |
disconnect() |
int |
getConnectTimeout() |
InputStream |
getErrorStream()
Returns an input stream from the server in the case of error such as the requested file (txt,
htm, html) is not found on the remote server.
|
String |
getHeaderField(int position)
Returns the value of the field at
position. |
String |
getHeaderField(String fieldName)
Returns the value of the field corresponding to the
fieldName, or null if there is no
such field. |
String |
getHeaderFieldKey(int position) |
Map<String,List<String>> |
getHeaderFields() |
InputStream |
getInputStream() |
boolean |
getInstanceFollowRedirects() |
OutputStream |
getOutputStream() |
Permission |
getPermission() |
int |
getReadTimeout() |
Map<String,List<String>> |
getRequestProperties() |
String |
getRequestProperty(String field) |
int |
getResponseCode() |
String |
getResponseMessage() |
void |
onFailure(Call call,
IOException e)
Called when the request could not be executed due to cancellation, a connectivity problem or
timeout.
|
void |
onResponse(Call call,
Response response)
Called when the HTTP response was successfully returned by the remote server.
|
void |
setConnectTimeout(int timeoutMillis) |
void |
setFixedLengthStreamingMode(int contentLength) |
void |
setFixedLengthStreamingMode(long contentLength) |
void |
setIfModifiedSince(long newValue) |
void |
setInstanceFollowRedirects(boolean followRedirects) |
void |
setReadTimeout(int timeoutMillis) |
void |
setRequestMethod(String method) |
void |
setRequestProperty(String field,
String newValue) |
boolean |
usingProxy()
Returns true if either:
A specific proxy was explicitly configured for this connection.
|
getFollowRedirects, getHeaderFieldDate, getRequestMethod, setChunkedStreamingMode, setFollowRedirectsgetAllowUserInteraction, getContent, getContent, getContentEncoding, getContentLength, getContentLengthLong, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderFieldInt, getHeaderFieldLong, getIfModifiedSince, getLastModified, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setUseCaches, toStringpublic static final String SELECTED_PROTOCOL
protocol ("spdy/3.1", "http/1.1",
etc).public static final String RESPONSE_SOURCE
public OkHttpURLConnection(URL url, OkHttpClient client)
public OkHttpURLConnection(URL url, OkHttpClient client, URLFilter urlFilter)
public void connect()
throws IOException
connect in class URLConnectionIOExceptionpublic void disconnect()
disconnect in class HttpURLConnectionpublic InputStream getErrorStream()
getErrorStream in class HttpURLConnectionpublic String getHeaderField(int position)
position. Returns null if there are fewer than position headers.getHeaderField in class HttpURLConnectionpublic String getHeaderField(String fieldName)
fieldName, or null if there is no
such field. If the field has multiple values, the last value is returned.getHeaderField in class URLConnectionpublic String getHeaderFieldKey(int position)
getHeaderFieldKey in class HttpURLConnectionpublic Map<String,List<String>> getHeaderFields()
getHeaderFields in class URLConnectionpublic Map<String,List<String>> getRequestProperties()
getRequestProperties in class URLConnectionpublic InputStream getInputStream() throws IOException
getInputStream in class URLConnectionIOExceptionpublic OutputStream getOutputStream() throws IOException
getOutputStream in class URLConnectionIOExceptionpublic Permission getPermission() throws IOException
getPermission in class HttpURLConnectionIOExceptionpublic String getRequestProperty(String field)
getRequestProperty in class URLConnectionpublic void setConnectTimeout(int timeoutMillis)
setConnectTimeout in class URLConnectionpublic void setInstanceFollowRedirects(boolean followRedirects)
setInstanceFollowRedirects in class HttpURLConnectionpublic boolean getInstanceFollowRedirects()
getInstanceFollowRedirects in class HttpURLConnectionpublic int getConnectTimeout()
getConnectTimeout in class URLConnectionpublic void setReadTimeout(int timeoutMillis)
setReadTimeout in class URLConnectionpublic int getReadTimeout()
getReadTimeout in class URLConnectionpublic boolean usingProxy()
selected in order to get it.
Warning: This method may return false before attempting to connect and true afterwards.
usingProxy in class HttpURLConnectionpublic String getResponseMessage() throws IOException
getResponseMessage in class HttpURLConnectionIOExceptionpublic int getResponseCode()
throws IOException
getResponseCode in class HttpURLConnectionIOExceptionpublic void setRequestProperty(String field, String newValue)
setRequestProperty in class URLConnectionpublic void setIfModifiedSince(long newValue)
setIfModifiedSince in class URLConnectionpublic void addRequestProperty(String field, String value)
addRequestProperty in class URLConnectionpublic void setRequestMethod(String method) throws ProtocolException
setRequestMethod in class HttpURLConnectionProtocolExceptionpublic void setFixedLengthStreamingMode(int contentLength)
setFixedLengthStreamingMode in class HttpURLConnectionpublic void setFixedLengthStreamingMode(long contentLength)
setFixedLengthStreamingMode in class HttpURLConnectionpublic void onFailure(Call call, IOException e)
Callbackpublic void onResponse(Call call, Response response)
CallbackResponse.body. The response is still live until
its response body is closed. The recipient of the callback may
consume the response body on another thread.
Note that transport-layer success (receiving a HTTP response code, headers and body) does
not necessarily indicate application-layer success: response may still indicate an
unhappy HTTP response code like 404 or 500.
onResponse in interface CallbackCopyright © 2017. All Rights Reserved.