本文整理匯總了Java中com.google.api.client.http.HttpRequest.setResponseInterceptor方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpRequest.setResponseInterceptor方法的具體用法?Java HttpRequest.setResponseInterceptor怎麽用?Java HttpRequest.setResponseInterceptor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.api.client.http.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.setResponseInterceptor方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initialize
import com.google.api.client.http.HttpRequest; //導入方法依賴的package包/類
@Override
public void initialize(HttpRequest request) throws IOException {
// Set a timeout for hanging-gets.
// TODO: Do this exclusively for work requests.
request.setReadTimeout(HANGING_GET_TIMEOUT_SEC * 1000);
LoggingHttpBackOffHandler loggingHttpBackOffHandler = new LoggingHttpBackOffHandler(
sleeper,
// Back off on retryable http errors and IOExceptions.
// A back-off multiplier of 2 raises the maximum request retrying time
// to approximately 5 minutes (keeping other back-off parameters to
// their default values).
new ExponentialBackOff.Builder().setNanoClock(nanoClock).setMultiplier(2).build(),
new ExponentialBackOff.Builder().setNanoClock(nanoClock).setMultiplier(2).build(),
ignoredResponseCodes
);
request.setUnsuccessfulResponseHandler(loggingHttpBackOffHandler);
request.setIOExceptionHandler(loggingHttpBackOffHandler);
// Set response initializer
if (responseInterceptor != null) {
request.setResponseInterceptor(responseInterceptor);
}
}
示例2: initialize
import com.google.api.client.http.HttpRequest; //導入方法依賴的package包/類
/**
* Initializes this {@link com.google.api.client.auth.oauth2.Credential} subclass. Adds an
* additional interceptor that properly sets the default encoding of the "application/json"
* media type to UTF-8 as required by <a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>.
*
* @param request The {@link HttpRequest} to be initialized.
*/
@Override
public void initialize(HttpRequest request) throws IOException {
// Initially, pass the initialization on to super.
super.initialize(request);
// Set the timeouts.
request.setConnectTimeout(3 * 600000); // 10 minutes connect timeout
request.setReadTimeout(3 * 600000); // 10 minutes read timeout
// Add an additional interceptor.
request.setResponseInterceptor(new HttpResponseInterceptor() {
@Override
public void interceptResponse(HttpResponse response) throws IOException {
// Decode JSON as UTF-8 if no charset is specified.
if (response.getContentType().equals("application/json") &&
response.getMediaType() != null &&
response.getMediaType().getCharsetParameter() == null)
{
response.getMediaType().setCharsetParameter(StandardCharsets.UTF_8);
}
}
});
// Add a custom handler for unsuccessful responses.
request.setUnsuccessfulResponseHandler(
new UnsuccessfulResponseHandler());
}