Tabnine Logo
Request$Builder.<init>
Code IndexAdd Tabnine to your IDE (free)

How to use
okhttp3.Request$Builder
constructor

Best Java code snippets using okhttp3.Request$Builder.<init> (Showing top 20 results out of 5,553)

Refine searchRefine arrow

  • Request.Builder.url
  • Request.Builder.build
  • OkHttpClient.newCall
  • Response.body
  • OkHttpClient.<init>
  • Call.execute
canonical example by Tabnine

public void sendGetRequest(String url) throws IOException {
 OkHttpClient client = new OkHttpClient();
 Request request = new Request.Builder().url(url).build();
 try (Response response = client.newCall(request).execute()) {
  String responseBody = response.body().string();
  // ... do something with response
 }
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("https://publicobject.com/helloworld.txt")
   .build();
 try (Response response = client.newCall(request).execute()) {
  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  System.out.println(response.body().string());
 }
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay.
   .build();
 try (Response response = client.newCall(request).execute()) {
  System.out.println("Response completed: " + response);
 }
}
origin: square/okhttp

public static void main(String... args) throws Exception {
 OkHttpClient client = new OkHttpClient();
 // Create request for remote resource.
 Request request = new Request.Builder()
   .url(ENDPOINT)
   .build();
 // Execute the request and retrieve the response.
 try (Response response = client.newCall(request).execute()) {
  // Deserialize HTTP response to concrete type.
  ResponseBody body = response.body();
  List<Contributor> contributors = CONTRIBUTORS_JSON_ADAPTER.fromJson(body.source());
  // Sort list by the most contributions.
  Collections.sort(contributors, (c1, c2) -> c2.contributions - c1.contributions);
  // Output list of contributors.
  for (Contributor contributor : contributors) {
   System.out.println(contributor.login + ": " + contributor.contributions);
  }
 }
}
origin: stackoverflow.com

 public static final MediaType JSON
  = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
 RequestBody body = RequestBody.create(JSON, json);
 Request request = new Request.Builder()
   .url(url)
   .post(body)
   .build();
 Response response = client.newCall(request).execute();
 return response.body().string();
}
origin: SonarSource/sonarqube

private static Response call(String url) throws IOException {
 Request request = new Request.Builder().get().url(url).build();
 return new OkHttpClient().newCall(request).execute();
}
origin: guolindev/booksource

public static void sendOkHttpRequest(String address, okhttp3.Callback callback) {
  OkHttpClient client = new OkHttpClient();
  Request request = new Request.Builder().url(address).build();
  client.newCall(request).enqueue(callback);
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("http://publicobject.com/helloworld.txt")
   .build();
 client.newCall(request).enqueue(new Callback() {
  @Override public void onFailure(Call call, IOException e) {
   e.printStackTrace();
  }
  @Override public void onResponse(Call call, Response response) throws IOException {
   try (ResponseBody responseBody = response.body()) {
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    Headers responseHeaders = response.headers();
    for (int i = 0, size = responseHeaders.size(); i < size; i++) {
     System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }
    System.out.println(responseBody.string());
   }
  }
 });
}
origin: square/okhttp

private void run() {
 OkHttpClient client = new OkHttpClient.Builder()
   .readTimeout(0,  TimeUnit.MILLISECONDS)
   .build();
 Request request = new Request.Builder()
   .url("ws://echo.websocket.org")
   .build();
 client.newWebSocket(request, this);
 // Trigger shutdown of the dispatcher's executor so this process can exit cleanly.
 client.dispatcher().executorService().shutdown();
}
origin: square/retrofit

/** Create a synthetic successful response with {@code body} as the deserialized body. */
public static <T> Response<T> success(@Nullable T body) {
 return success(body, new okhttp3.Response.Builder() //
   .code(200)
   .message("OK")
   .protocol(Protocol.HTTP_1_1)
   .request(new Request.Builder().url("http://localhost/").build())
   .build());
}
origin: square/retrofit

 @Override public Request request() {
  if (response != null) {
   return response.raw().request();
  }
  return new Request.Builder()
    .url("http://localhost")
    .build();
 }
}
origin: square/okhttp

/** See https://api.slack.com/rtm. */
public WebSocket rtm(HttpUrl url, WebSocketListener listener) {
 return httpClient.newWebSocket(new Request.Builder()
   .url(url)
   .build(), listener);
}
origin: square/okhttp

private Request buildRequest(String hostname, int type) {
 Request.Builder requestBuilder = new Request.Builder().header("Accept", DNS_MESSAGE.toString());
 ByteString query = DnsRecordCodec.encodeQuery(hostname, type);
 if (post) {
  requestBuilder = requestBuilder.url(url).post(RequestBody.create(DNS_MESSAGE, query));
 } else {
  String encoded = query.base64Url().replace("=", "");
  HttpUrl requestUrl = url.newBuilder().addQueryParameter("dns", encoded).build();
  requestBuilder = requestBuilder.url(requestUrl);
 }
 return requestBuilder.build();
}
origin: square/retrofit

/**
 * Create a synthetic error response with an HTTP status code of {@code code} and {@code body}
 * as the error body.
 */
public static <T> Response<T> error(int code, ResponseBody body) {
 if (code < 400) throw new IllegalArgumentException("code < 400: " + code);
 return error(body, new okhttp3.Response.Builder() //
   .code(code)
   .message("Response.error()")
   .protocol(Protocol.HTTP_1_1)
   .request(new Request.Builder().url("http://localhost/").build())
   .build());
}
origin: square/okhttp

private WebSocket newWebSocket(String path, WebSocketListener listener) {
 Request request = new Request.Builder().url(HOST + path).build();
 return client.newWebSocket(request, listener);
}
origin: square/retrofit

/**
 * Create a synthetic successful response using {@code headers} with {@code body} as the
 * deserialized body.
 */
public static <T> Response<T> success(@Nullable T body, Headers headers) {
 checkNotNull(headers, "headers == null");
 return success(body, new okhttp3.Response.Builder() //
   .code(200)
   .message("OK")
   .protocol(Protocol.HTTP_1_1)
   .headers(headers)
   .request(new Request.Builder().url("http://localhost/").build())
   .build());
}
origin: square/retrofit

/**
 * Create a synthetic successful response with an HTTP status code of {@code code} and
 * {@code body} as the deserialized body.
 */
public static <T> Response<T> success(int code, @Nullable T body) {
 if (code < 200 || code >= 300) {
  throw new IllegalArgumentException("code < 200 or >= 300: " + code);
 }
 return success(body, new okhttp3.Response.Builder() //
   .code(code)
   .message("Response.success()")
   .protocol(Protocol.HTTP_1_1)
   .request(new Request.Builder().url("http://localhost/").build())
   .build());
}
origin: spring-projects/spring-framework

static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method)
    throws MalformedURLException {
  okhttp3.MediaType contentType = getContentType(headers);
  RequestBody body = (content.length > 0 ||
      okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ?
      RequestBody.create(contentType, content) : null);
  Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body);
  headers.forEach((headerName, headerValues) -> {
    for (String headerValue : headerValues) {
      builder.addHeader(headerName, headerValue);
    }
  });
  return builder.build();
}
origin: square/okhttp

Request createRequest() {
 Request.Builder request = new Request.Builder();
 request.url(url);
 request.method(getRequestMethod(), getRequestBody());
 if (headers != null) {
  for (String header : headers) {
   String[] parts = header.split(":", 2);
   request.header(parts[0], parts[1]);
  }
 }
 if (referer != null) {
  request.header("Referer", referer);
 }
 request.header("User-Agent", userAgent);
 return request.build();
}
origin: square/okhttp

String run(String url) throws IOException {
 Request request = new Request.Builder()
   .url(url)
   .build();
 try (Response response = client.newCall(request).execute()) {
  return response.body().string();
 }
}
okhttp3Request$Builder<init>

Popular methods of Request$Builder

  • build
  • url
  • addHeader
    Adds a header with name and value. Prefer this method for multiply-valued headers like "Cookie".Note
  • post
  • header
    Sets the header named name to value. If this request already has any headers with that name, they ar
  • get
  • method
  • cacheControl
    Sets this request's Cache-Control header, replacing any cache control headers already present. If ca
  • delete
  • put
  • headers
    Removes all headers on this builder and adds headers.
  • removeHeader
    Removes all headers named name on this builder.
  • headers,
  • removeHeader,
  • tag,
  • patch,
  • head

Popular in Java

  • Start an intent from android
  • getContentResolver (Context)
  • addToBackStack (FragmentTransaction)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • URI (java.net)
    A Uniform Resource Identifier that identifies an abstract or physical resource, as specified by RFC
  • Permission (java.security)
    Legacy security code; do not use.
  • Properties (java.util)
    A Properties object is a Hashtable where the keys and values must be Strings. Each property can have
  • Stack (java.util)
    Stack is a Last-In/First-Out(LIFO) data structure which represents a stack of objects. It enables u
  • SSLHandshakeException (javax.net.ssl)
    The exception that is thrown when a handshake could not be completed successfully.
  • Project (org.apache.tools.ant)
    Central representation of an Ant project. This class defines an Ant project with all of its targets,
  • Top plugins for Android Studio
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now