Class HttpTransport
- java.lang.Object
-
- com.google.api.client.http.HttpTransport
-
- Direct Known Subclasses:
ApacheHttpTransport,MockHttpTransport,NetHttpTransport
public abstract class HttpTransport extends Object
Thread-safe abstract HTTP transport.Implementation is thread-safe, and sub-classes must be thread-safe. For maximum efficiency, applications should use a single globally-shared instance of the HTTP transport.
The recommended concrete implementation HTTP transport library to use depends on what environment you are running in:
- Google App Engine: use
com.google.api.client.extensions.appengine.http.UrlFetchTransport.com.google.api.client.apache.ApacheHttpTransportdoesn't work on App Engine because the Apache HTTP Client opens its own sockets (though in theory there are ways to hack it to work on App Engine that might work).com.google.api.client.javanet.NetHttpTransportis discouraged due to a bug in the App Engine SDK itself in how it parses HTTP headers in the response.
- Android:
- For maximum backwards compatibility with older SDK's use
newCompatibleTransportfromcom.google.api.client.extensions.android.http.AndroidHttp(read its JavaDoc for details). - If your application is targeting Gingerbread (SDK 2.3) or higher, simply use
com.google.api.client.javanet.NetHttpTransport.
- For maximum backwards compatibility with older SDK's use
- Other Java environments
com.google.api.client.javanet.NetHttpTransportis based on the HttpURLConnection built into the Java SDK, so it is normally the preferred choice.com.google.api.client.apache.ApacheHttpTransportis a good choice for users of the Apache HTTP Client, especially if you need some of the configuration options available in that library.
Some HTTP transports do not support all HTTP methods. Use
supportsMethod(String)to check if a certain HTTP method is supported. CallingbuildRequest()on a method that is not supported will result in anIllegalArgumentException.Subclasses should override
supportsMethod(String)andbuildRequest(String, String)to build requests and specify which HTTP methods are supported.- Since:
- 1.0
- Author:
- Yaniv Inbar
-
-
Constructor Summary
Constructors Constructor Description HttpTransport()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract LowLevelHttpRequestbuildRequest(String method, String url)Builds a low level HTTP request for the given HTTP method.HttpRequestFactorycreateRequestFactory()Returns a new instance of an HTTP request factory based on this HTTP transport.HttpRequestFactorycreateRequestFactory(HttpRequestInitializer initializer)Returns a new instance of an HTTP request factory based on this HTTP transport with the given HTTP request initializer.voidshutdown()Default implementation does nothing, but subclasses may override to possibly release allocated system resources or close connections.booleansupportsMethod(String method)Returns whether a specified HTTP method is supported by this transport.
-
-
-
Method Detail
-
createRequestFactory
public final HttpRequestFactory createRequestFactory()
Returns a new instance of an HTTP request factory based on this HTTP transport.- Returns:
- new instance of an HTTP request factory
- Since:
- 1.4
-
createRequestFactory
public final HttpRequestFactory createRequestFactory(HttpRequestInitializer initializer)
Returns a new instance of an HTTP request factory based on this HTTP transport with the given HTTP request initializer.- Parameters:
initializer- HTTP request initializer ornullfor none- Returns:
- new instance of an HTTP request factory
- Since:
- 1.4
-
supportsMethod
public boolean supportsMethod(String method) throws IOException
Returns whether a specified HTTP method is supported by this transport.Default implementation returns true if and only if the request method is
"DELETE","GET","POST", or"PUT". Subclasses should override.- Parameters:
method- HTTP method- Throws:
IOException- I/O exception- Since:
- 1.12
-
buildRequest
protected abstract LowLevelHttpRequest buildRequest(String method, String url) throws IOException
Builds a low level HTTP request for the given HTTP method.- Parameters:
method- HTTP methodurl- URL- Returns:
- new low level HTTP request
- Throws:
IllegalArgumentException- if HTTP method is not supportedIOException- Since:
- 1.12
-
shutdown
public void shutdown() throws IOExceptionDefault implementation does nothing, but subclasses may override to possibly release allocated system resources or close connections.- Throws:
IOException- I/O exception- Since:
- 1.4
-
-