本文整理汇总了Java中retrofit.client.Request.getBody方法的典型用法代码示例。如果您正苦于以下问题:Java Request.getBody方法的具体用法?Java Request.getBody怎么用?Java Request.getBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类retrofit.client.Request
的用法示例。
在下文中一共展示了Request.getBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: getRequestInfo
import retrofit.client.Request; //导入方法依赖的package包/类
private static Profiler.RequestInformation getRequestInfo(String serverUrl,
RestMethodInfo methodDetails, Request request)
{
long contentLength = 0;
String contentType = null;
TypedOutput body = request.getBody();
if (body != null)
{
contentLength = body.length();
contentType = body.mimeType();
}
return new Profiler.RequestInformation(methodDetails.requestMethod, serverUrl,
methodDetails.requestUrl, contentLength, contentType);
}
示例4: multipartNullRemovesPart
import retrofit.client.Request; //导入方法依赖的package包/类
@Test
public void multipartNullRemovesPart() throws Exception
{
Request request = new Helper() //
.setMethod("POST") //
.setHasBody() //
.setUrl("http://example.com") //
.setPath("/foo/bar/") //
.setMultipart() //
.addPart("ping", "pong") //
.addPart("fizz", null) //
.build();
assertThat(request.getMethod()).isEqualTo("POST");
assertThat(request.getHeaders()).isEmpty();
assertThat(request.getUrl()).isEqualTo("http://example.com/foo/bar/");
MultipartTypedOutput body = (MultipartTypedOutput) request.getBody();
List<byte[]> bodyParts = MimeHelper.getParts(body);
assertThat(bodyParts).hasSize(1);
Iterator<byte[]> iterator = bodyParts.iterator();
String one = new String(iterator.next(), "UTF-8");
assertThat(one).contains("name=\"ping\"").endsWith("\r\npong");
}
示例5: 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);
}
示例6: 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());
}
示例7: getRequestInfo
import retrofit.client.Request; //导入方法依赖的package包/类
private static Profiler.RequestInformation getRequestInfo(String serverUrl, RestMethodInfo methodDetails, Request request) {
long contentLength = 0;
String contentType = null;
TypedOutput body = request.getBody();
if (body != null) {
contentLength = body.length();
contentType = body.mimeType();
}
return new Profiler.RequestInformation(methodDetails.requestMethod, serverUrl, methodDetails.requestUrl, contentLength, contentType);
}
示例8: 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;
}
}
示例9: 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;
}
示例10: sendBody
import retrofit.client.Request; //导入方法依赖的package包/类
public static void sendBody(final Request request, final HttpRequest httpRequest) throws IOException {
final String requestMethod = request.getMethod();
final TypedOutput requestBody = request.getBody();
// Only POST and PUT has body as far as I know
if ((METHOD_POST.equals(requestMethod)) || METHOD_PUT.equals(requestMethod)) {
if (requestBody != null) {
// Content-Type
httpRequest.contentType(requestBody.mimeType());
// Payload
ByteArrayOutputStream outputBytes = new ByteArrayOutputStream();
requestBody.writeTo(outputBytes);
byte[] dataBytes = outputBytes.toByteArray();
try {
httpRequest.send(dataBytes);
} catch (HttpRequest.HttpRequestException hre) {
// Throw original IOException since Retrofit handles this
throw hre.getCause();
}
}
}
}
示例11: simpleMultipart
import retrofit.client.Request; //导入方法依赖的package包/类
@Test
public void simpleMultipart() throws Exception
{
Request request = new Helper() //
.setMethod("POST") //
.setHasBody() //
.setUrl("http://example.com") //
.setPath("/foo/bar/") //
.setMultipart() //
.addPart("ping", "pong") //
.addPart("kit", new TypedString("kat")) //
.build();
assertThat(request.getMethod()).isEqualTo("POST");
assertThat(request.getHeaders()).isEmpty();
assertThat(request.getUrl()).isEqualTo("http://example.com/foo/bar/");
MultipartTypedOutput body = (MultipartTypedOutput) request.getBody();
List<byte[]> bodyParts = MimeHelper.getParts(body);
assertThat(bodyParts).hasSize(2);
Iterator<byte[]> iterator = bodyParts.iterator();
String one = new String(iterator.next(), "UTF-8");
assertThat(one).contains("name=\"ping\"\r\n").endsWith("\r\npong");
String two = new String(iterator.next(), "UTF-8");
assertThat(two).contains("name=\"kit\"").endsWith("\r\nkat");
}
示例12: 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;
}
示例13: execute
import retrofit.client.Request; //导入方法依赖的package包/类
@Override
public Response execute( Request request ) throws IOException
{
if ( !isUserLoggedIn() )
{
throw new IOException( "User not logged in" );
}
// First, sign the request
OAuthRequest oAuthRequest = new OAuthRequest( getVerbForMethod( request.getMethod() ), request.getUrl() );
signingService.signRequest( getAccessToken(), oAuthRequest );
// Then switch the old headers with signed headers
Map<String, String> signedHeadersMap = oAuthRequest.getHeaders();
List<Header> signedHeaders = new ArrayList<Header>( signedHeadersMap.size() );
for ( Map.Entry<String, String> header : signedHeadersMap.entrySet() )
{
signedHeaders.add( new Header( header.getKey(), header.getValue() ) );
}
// Switch old url with new url
String signedUrl = oAuthRequest.getCompleteUrl();
Helper.debug( signedUrl );
// Make new request with signed headers and url
Request signedRequest = new Request( request.getMethod(), signedUrl, signedHeaders, request.getBody() );
// We need to check status of response in case it failed due to access token expiry
Response response = super.execute( signedRequest );
// 401 most likely indicates that access token has expired.
// Time to refresh and resend the request
if ( response.getStatus() == 401 && accessTokenCanExpire() )
{
// Do ensure that updateAccessToken() stores the value
// of the updated access token in AccountPrefs.
updateAccessToken();
return execute( request );
}
return response;
}
示例14: featureValueOf
import retrofit.client.Request; //导入方法依赖的package包/类
@Override
protected TypedOutput featureValueOf(Request actual) {
final TypedOutput body = actual.getBody();
return body;
}
示例15: 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;
}