本文整理汇总了Java中retrofit.client.Request类的典型用法代码示例。如果您正苦于以下问题:Java Request类的具体用法?Java Request怎么用?Java Request使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Request类属于retrofit.client包,在下文中一共展示了Request类的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: post
import retrofit.client.Request; //导入依赖的package包/类
@Test public void post() throws IOException {
TypedString body = new TypedString("hi");
Request request = new Request("POST", HOST + "/foo/bar/", null, body);
okhttp3.Request okRequest = Ok3Client.createRequest(request);
assertThat(okRequest.method()).isEqualTo("POST");
assertThat(okRequest.url().toString()).isEqualTo(HOST + "/foo/bar/");
assertThat(okRequest.headers().size()).isEqualTo(0);
RequestBody okBody = okRequest.body();
assertThat(okBody).isNotNull();
Buffer buffer = new Buffer();
okBody.writeTo(buffer);
assertThat(buffer.readUtf8()).isEqualTo("hi");
}
示例3: responseNoContentType
import retrofit.client.Request; //导入依赖的package包/类
@Test public void responseNoContentType() throws IOException {
okhttp3.Response okResponse = new okhttp3.Response.Builder()
.code(200).message("OK")
.body(new TestResponseBody("hello", null))
.addHeader("foo", "bar")
.addHeader("kit", "kat")
.protocol(Protocol.HTTP_1_1)
.request(new okhttp3.Request.Builder()
.url(HOST + "/foo/bar/")
.get()
.build())
.build();
Response response = Ok3Client.parseResponse(okResponse);
assertThat(response.getUrl()).isEqualTo(HOST + "/foo/bar/");
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getReason()).isEqualTo("OK");
assertThat(response.getHeaders()) //
.containsExactly(new Header("foo", "bar"), new Header("kit", "kat"));
TypedInput responseBody = response.getBody();
assertThat(responseBody.mimeType()).isNull();
assertThat(buffer(source(responseBody.in())).readUtf8()).isEqualTo("hello");
}
示例4: emptyResponse
import retrofit.client.Request; //导入依赖的package包/类
@Test public void emptyResponse() throws IOException {
okhttp3.Response okResponse = new okhttp3.Response.Builder()
.code(200)
.message("OK")
.body(new TestResponseBody("", null))
.addHeader("foo", "bar")
.addHeader("kit", "kat")
.protocol(Protocol.HTTP_1_1)
.request(new okhttp3.Request.Builder()
.url(HOST + "/foo/bar/")
.get()
.build())
.build();
Response response = Ok3Client.parseResponse(okResponse);
assertThat(response.getUrl()).isEqualTo(HOST + "/foo/bar/");
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getReason()).isEqualTo("OK");
assertThat(response.getHeaders()) //
.containsExactly(new Header("foo", "bar"), new Header("kit", "kat"));
assertThat(response.getBody()).isNull();
}
示例5: execute
import retrofit.client.Request; //导入依赖的package包/类
@Override
public Response execute(Request request) throws IOException {
try {
if (!ConnectivityUtil.isConnected(context)) {
throw RetrofitError.unexpectedError("Nincs internet", new NoConnectivityException("No Internet"));
} else {
Response r = wrappedClient.execute(request);
checkResult(r);
return r;
}
} catch (RetrofitError retrofitError) {
if (retry(retrofitError, retries)) {
return execute(request);
} else {
throw new ConnectionError();
}
} catch (Exception e) {
throw new ConnectionError();
}
}
示例6: build
import retrofit.client.Request; //导入依赖的package包/类
Request build() throws UnsupportedEncodingException {
String apiUrl = this.apiUrl;
StringBuilder url = new StringBuilder(apiUrl);
if (apiUrl.endsWith("/")) {
// We require relative paths to start with '/'. Prevent a double-slash.
url.deleteCharAt(url.length() - 1);
}
url.append(relativeUrl);
StringBuilder queryParams = this.queryParams;
if (queryParams.length() > 0) {
url.append(queryParams);
}
if (multipartBody != null && multipartBody.getPartCount() == 0) {
throw new IllegalStateException("Multipart requests must contain at least one part.");
}
return new Request(requestMethod, url.toString(), headers, body);
}
示例7: 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);
}
}
示例8: execute
import retrofit.client.Request; //导入依赖的package包/类
@Override
public Response execute(Request request) throws IOException {
List<Matcher<? super Request>> unmatchedRoutes = new LinkedList<>();
for (Route route : routes) {
if (route.requestMatcher.matches(request)) return route.response.createFrom(request);
unmatchedRoutes.add(route.requestMatcher);
}
StringDescription description = new StringDescription();
AnyOf.anyOf(unmatchedRoutes).describeTo(description);
return new Response(
request.getUrl(),
404,
"No route matched",
Collections.<Header>emptyList(),
new TypedString("No matching route found. expected:\n" + description.toString())
);
}
示例9: errorWithEmptyBodyDoesNotCrash
import retrofit.client.Request; //导入依赖的package包/类
@Test
public void errorWithEmptyBodyDoesNotCrash() {
TestClient client = new RestAdapter.Builder()
.setEndpoint("http://example.com")
.setClient(new Client() {
@Override
public Response execute(Request request) throws IOException {
return new Response("", 400, "invalid request", Collections.<Header>emptyList(), null);
}
})
.setErrorHandler(errorHandler)
.setExecutors(new SynchronousExecutor(), new SynchronousExecutor())
.build()
.create(TestClient.class);
try {
client.getFullMenus();
failBecauseExceptionWasNotThrown(ApiException.class);
} catch (ApiException e) {
assertThat(e.getMessage()).isNull();
assertThat(e.getCause()).isNotNull();
}
}
示例10: UnauthorizedOnNonAuthRouteThrowsCorrectException
import retrofit.client.Request; //导入依赖的package包/类
@Test
public void UnauthorizedOnNonAuthRouteThrowsCorrectException() {
TestClient client = new RestAdapter.Builder()
.setEndpoint("http://example.com")
.setClient(new Client() {
@Override
public Response execute(Request request) throws IOException {
Error apiError = new Error("1234", "Invalid social credentials");
TypedInput input = new TypedByteArray("application/x-protobuf", apiError.toByteArray());
return new Response("", 401, "invalid request", Collections.<Header>emptyList(), input);
}
})
.setErrorHandler(errorHandler)
.setExecutors(new SynchronousExecutor(), new SynchronousExecutor())
.setConverter(new WireConverter())
.build()
.create(TestClient.class);
try {
client.getFullMenus();
failBecauseExceptionWasNotThrown(ExpiredSessionException.class);
} catch (ExpiredSessionException e) {
// No-op
}
}
示例11: unauthorizedOnAuthRouteThrowsCorrectException
import retrofit.client.Request; //导入依赖的package包/类
@Test
public void unauthorizedOnAuthRouteThrowsCorrectException() {
AuthService service = new RestAdapter.Builder()
.setEndpoint("http://example.com")
.setClient(new Client() {
@Override
public Response execute(Request request) throws IOException {
Error apiError = new Error("1234", "Invalid social credentials");
TypedInput input = new TypedByteArray("application/x-protobuf", apiError.toByteArray());
return new Response("", 401, "invalid request", Collections.<Header>emptyList(), input);
}
})
.setErrorHandler(errorHandler)
.setExecutors(new SynchronousExecutor(), new SynchronousExecutor())
.setConverter(new WireConverter())
.build()
.create(AuthService.class);
try {
service.getAuth(new AuthRequest());
failBecauseExceptionWasNotThrown(SocialCredentialsException.class);
} catch (SocialCredentialsException e) {
// No-op
}
}
示例12: testSigningClient
import retrofit.client.Request; //导入依赖的package包/类
/**
* Test signing client.
*/
@Test
public void testSigningClient() {
LOG.info("Signing Client test");
// check that the signing process does what it's supposed to do
SigningClient sc = new SigningClient(new MockClient(), null, null, null, new ExceptionHandler() {
public Throwable handleError(RetrofitError arg0) {
return null;
}
public void handleException(Exception exception) {
}
});
try {
Response rs = sc.execute(new Request("GET", "/test", Collections.EMPTY_LIST, new TypedByteArray("application/json", "".getBytes())));
assertNotNull(rs);
} catch (IOException e) {
LOG.error("Signing Client test error", e);
}
}
示例13: testCredentialClient
import retrofit.client.Request; //导入依赖的package包/类
/**
* Test credential client.
*/
@Test
public void testCredentialClient() {
LOG.info("Credential Client test");
// check that the signing process does what it's supposed to do
CredentialClient cc = new CredentialClient(new MockAuthClient(), null, new ExceptionHandler() {
public Throwable handleError(RetrofitError arg0) {
return null;
}
public void handleException(Exception exception) {
}
});
try {
Response rs = cc.execute(new Request("GET", "/test", Collections.EMPTY_LIST, new TypedByteArray("application/json", "".getBytes())));
assertNotNull(rs);
} catch (IOException e) {
LOG.error("Signing Client test error", e);
}
}
示例14: 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;
}
示例15: testPOSTDataEcho
import retrofit.client.Request; //导入依赖的package包/类
@Test
public void testPOSTDataEcho() throws Exception {
// Given
final String postBodyString = "hello";
HttpRequestClient httpRequestClient = new HttpRequestClient();
TypedOutput postBody = new TypedString(postBodyString);
Request request = new Request("POST", HTTP_BIN_ROOT + "/post", null, postBody);
// When
final Response response = httpRequestClient.execute(request);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> jsonObj = objectMapper.readValue(response.getBody().in(), Map.class);
// Then
assertNotNull(response);
assertThat(response.getStatus(), is(200));
assertThat(jsonObj.get("data").toString(), is(postBodyString));
}