本文整理汇总了Java中retrofit.client.Request.getHeaders方法的典型用法代码示例。如果您正苦于以下问题:Java Request.getHeaders方法的具体用法?Java Request.getHeaders怎么用?Java Request.getHeaders使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类retrofit.client.Request
的用法示例。
在下文中一共展示了Request.getHeaders方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createRequest
import retrofit.client.Request; //导入方法依赖的package包/类
static okhttp3.Request createRequest(Request request) {
RequestBody requestBody;
if (requiresRequestBody(request.getMethod()) && request.getBody() == null) {
requestBody = RequestBody.create(null, NO_BODY);
} else {
requestBody = createRequestBody(request.getBody());
}
okhttp3.Request.Builder builder = new okhttp3.Request.Builder()
.url(request.getUrl())
.method(request.getMethod(), requestBody);
List<Header> headers = request.getHeaders();
for (int i = 0, size = headers.size(); i < size; i++) {
Header header = headers.get(i);
String value = header.getValue();
if (value == null) {
value = "";
}
builder.addHeader(header.getName(), value);
}
return builder.build();
}
示例2: execute
import retrofit.client.Request; //导入方法依赖的package包/类
@Override
public Response execute(Request request) throws IOException {
try {
List<Header> headers = new LinkedList<Header>(request.getHeaders());
// if logged in add auth header
Optional<Account> account = loginManager.getAccount();
if (account.isPresent()) {
String token = loginManager.getToken(account.get());
Header authHeader = new Header("Authorization", "Bearer " + token);
headers.add(authHeader);
}
Request signedRequest = new Request(
request.getMethod(),
request.getUrl(),
headers,
request.getBody());
return super.execute(signedRequest);
} catch (GoogleAuthException gae) {
throw new IOException(gae);
}
}
示例3: prepareHttpRequest
import retrofit.client.Request; //导入方法依赖的package包/类
public static HttpRequest prepareHttpRequest(final Request request) throws IOException {
// Extract details from incoming request from Retrofit
final String requestUrl = request.getUrl();
final String requestMethod = request.getMethod();
final List<Header> requestHeaders = request.getHeaders();
// URL and Method
final HttpRequest httpRequest = new HttpRequest(requestUrl, requestMethod);
// Headers
for (Header header: requestHeaders) {
httpRequest.header(header.getName(), header.getValue());
}
return httpRequest;
}
示例4: execute
import retrofit.client.Request; //导入方法依赖的package包/类
@Override
public Response execute(Request request) throws IOException {
Map<String, String> headersMap = MutableMap.of();
for (Header header: request.getHeaders()) {
headersMap.put(header.getName(), header.getValue());
}
String mimeType = "text/plain"; // Default Content-Type used in Brooklyn Ambari
byte[] content = null;
TypedOutput body = request.getBody();
if (body != null) {
mimeType = request.getBody().mimeType();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
body.writeTo(baos);
content = baos.toByteArray();
} finally {
Streams.closeQuietly(baos);
}
}
final HttpResponse response = httpExecutor.execute(new HttpRequest.Builder()
.headers(headersMap)
.uri(URI.create(request.getUrl()))
.method(request.getMethod())
.body(content)
.build());
List<Header> responseHeaders = Lists.newArrayList();
for (Map.Entry<String, String> entry: response.headers().entries()) {
responseHeaders.add(new Header(entry.getKey(), entry.getValue()));
}
TypedInput responseBody = new TypedByteArray(mimeType, ByteStreams.toByteArray(response.getContent()));
return new Response(request.getUrl(),
response.code(),
(response.reasonPhrase() != null) ? response.reasonPhrase() : "",
responseHeaders,
responseBody);
}
示例5: execute
import retrofit.client.Request; //导入方法依赖的package包/类
@Override
public Response execute(Request request) throws IOException {
List<Header> headers = request.getHeaders();
CurlHttp curlHttp = CurlHttp.newInstance();
if (callback != null) {
callback.afterInit(curlHttp, request.getUrl());
}
if (headers != null && headers.size() > 0) {
for (Header header : headers) {
Log.v(TAG, "add header: " + header.getName() + " " + header.getValue());
curlHttp.addHeader(header.getName(), header.getValue());
}
}
if ("get".equalsIgnoreCase(request.getMethod())) {
// get
curlHttp.getUrl(request.getUrl());
} else {
// post
TypedOutput body = request.getBody();
if (body != null) {
Log.v(TAG, "set request body");
ByteArrayOutputStream os = new ByteArrayOutputStream((int) body.length());
body.writeTo(os);
curlHttp.setBody(body.mimeType(), os.toByteArray());
}
curlHttp.postUrl(request.getUrl());
}
return convertResult(request, curlHttp.perform());
}
示例6: execute
import retrofit.client.Request; //导入方法依赖的package包/类
@Override
public Response execute(Request request) throws IOException {
Response response = super.execute(request);
if (response.getStatus() == 401) {
// Unauthorized, attempt to refresh access token
String refreshToken = mAuthModel.getRefreshToken();
if (refreshToken == null) {
return response;
}
mAuthModel = sAuthService.refreshToken(refreshToken);
mAuthModel.setContext(mContext);
mAuthModel.saveTokens();
// Switch out auth token in request
List<Header> headers = new ArrayList<Header>();
for (Header header : request.getHeaders()) {
if (header.getName().equals("Authorization")) {
headers.add(new Header("Authorization", "Bearer " + mAuthModel.getUserToken()));
} else {
headers.add(header);
}
}
// Retry request
Request updatedRequest = new Request(request.getMethod(), request.getUrl(), headers, request.getBody());
return super.execute(updatedRequest);
} else {
return response;
}
}
示例7: sign
import retrofit.client.Request; //导入方法依赖的package包/类
/**
* internal sign function of the request.
*
* @param request the request
* @return the request
*/
private Request sign(final Request request) {
if (request != null && request.getHeaders() != null) {
try {
List<Header> modifiedHeaders = new ArrayList<Header>();
// duplicate the headers if there is already something
if (request.getHeaders().isEmpty()) {
for (int i = 0; i < request.getHeaders().size(); i++) {
Header header = request.getHeaders().get(i);
// avoid duplication otherwise it will repeted forever
if (!modifiedHeaders.contains(header)) {
modifiedHeaders.add(header);
}
}
}
// signing thing
modifiedHeaders.add(new Header("X-Ovh-Application", applicationKey));
modifiedHeaders.add(new Header("Content-type", "application/json"));
return new Request(request.getMethod(), request.getUrl(), modifiedHeaders, request.getBody());
} catch (Exception e) {
exceptionHandler.handleException(e);
}
}
return request;
}
示例8: logAndReplaceRequest
import retrofit.client.Request; //导入方法依赖的package包/类
/**
* Log request headers and body. Consumes request body and returns identical replacement.
*/
private Request logAndReplaceRequest(Request request) throws IOException {
log.log(String.format("---> HTTP %s %s", request.getMethod(), request.getUrl()));
if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal()) {
for (Header header : request.getHeaders()) {
log.log(header.getName() + ": " + header.getValue());
}
long bodySize = 0;
TypedOutput body = request.getBody();
if (body != null) {
bodySize = body.length();
String bodyMime = body.mimeType();
if (bodyMime != null) {
log.log("Content-Type: " + bodyMime);
}
if (bodySize != -1) {
log.log("Content-Length: " + bodySize);
}
if (logLevel.ordinal() >= LogLevel.FULL.ordinal()) {
if (!request.getHeaders().isEmpty()) {
log.log("");
}
if (!(body instanceof TypedByteArray)) {
// Read the entire response body to we can log it and replace the original response
request = Utils.readBodyToBytesIfNecessary(request);
body = request.getBody();
}
byte[] bodyBytes = ((TypedByteArray) body).getBytes();
bodySize = bodyBytes.length;
String bodyCharset = MimeUtil.parseCharset(bodyMime);
String bodyString = new String(bodyBytes, bodyCharset);
for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
int end = Math.min(len, i + LOG_CHUNK_SIZE);
log.log(bodyString.substring(i, end));
}
}
}
log.log(String.format("---> END HTTP (%s-byte body)", bodySize));
}
return request;
}
示例9: featureValueOf
import retrofit.client.Request; //导入方法依赖的package包/类
@Override
protected List<Header> featureValueOf(Request request) {
return request.getHeaders();
}
示例10: logAndReplaceRequest
import retrofit.client.Request; //导入方法依赖的package包/类
/**
* Log request headers and body. Consumes request body and returns identical replacement.
*/
Request logAndReplaceRequest(String name, Request request) throws IOException
{
log.log(String.format("---> %s %s %s", name, request.getMethod(), request.getUrl()));
if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal())
{
for (Header header : request.getHeaders())
{
log.log(header.toString());
}
long bodySize = 0;
TypedOutput body = request.getBody();
if (body != null)
{
bodySize = body.length();
String bodyMime = body.mimeType();
if (bodyMime != null)
{
log.log("Content-Type: " + bodyMime);
}
if (bodySize != -1)
{
log.log("Content-Length: " + bodySize);
}
if (logLevel.ordinal() >= LogLevel.FULL.ordinal())
{
if (!request.getHeaders().isEmpty())
{
log.log("");
}
if (!(body instanceof TypedByteArray))
{
// Read the entire response body to we can log it and replace the original response
request = Utils.readBodyToBytesIfNecessary(request);
body = request.getBody();
}
byte[] bodyBytes = ((TypedByteArray) body).getBytes();
bodySize = bodyBytes.length;
String bodyCharset = MimeUtil.parseCharset(bodyMime);
log.log(new String(bodyBytes, bodyCharset));
}
}
log.log(String.format("---> END %s (%s-byte body)", name, bodySize));
}
return request;
}