Tabnine Logo
Call.execute
Code IndexAdd Tabnine to your IDE (free)

How to use
execute
method
in
okhttp3.Call

Best Java code snippets using okhttp3.Call.execute (Showing top 20 results out of 4,356)

Refine searchRefine arrow

  • OkHttpClient.newCall
  • Request.Builder.build
  • Request.Builder.url
  • Request.Builder.<init>
  • Response.body
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

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();
 }
}
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: openzipkin/brave

@Override protected void get(Call.Factory client) throws Exception {
 client.newCall(new Request.Builder().url(baseUrl()).build()).execute().body().close();
}
origin: square/okhttp

String post(String url, String json) throws IOException {
 RequestBody body = RequestBody.create(JSON, json);
 Request request = new Request.Builder()
   .url(url)
   .post(body)
   .build();
 try (Response response = client.newCall(request).execute()) {
  return response.body().string();
 }
}
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()) {
  System.out.println(response.request().header("Date"));
 }
}
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("https://api.github.com/repos/square/okhttp/issues")
   .header("User-Agent", "OkHttp Headers.java")
   .addHeader("Accept", "application/json; q=0.5")
   .addHeader("Accept", "application/vnd.github.v3+json")
   .build();
 try (Response response = client.newCall(request).execute()) {
  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  System.out.println("Server: " + response.header("Server"));
  System.out.println("Date: " + response.header("Date"));
  System.out.println("Vary: " + response.headers("Vary"));
 }
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("https://publicobject.com/secrets/hellosecret.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/1") // This URL is served with a 1 second delay.
   .build();
 // Copy to customize OkHttp for this request.
 OkHttpClient client1 = client.newBuilder()
   .readTimeout(500, TimeUnit.MILLISECONDS)
   .build();
 try (Response response = client1.newCall(request).execute()) {
  System.out.println("Response 1 succeeded: " + response);
 } catch (IOException e) {
  System.out.println("Response 1 failed: " + e);
 }
 // Copy to customize OkHttp for this request.
 OkHttpClient client2 = client.newBuilder()
   .readTimeout(3000, TimeUnit.MILLISECONDS)
   .build();
 try (Response response = client2.newCall(request).execute()) {
  System.out.println("Response 2 succeeded: " + response);
 } catch (IOException e) {
  System.out.println("Response 2 failed: " + e);
 }
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("http://publicobject.com/secrets/hellosecret.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("https://publicobject.com/robots.txt")
   .build();
 try (Response response = client.newCall(request).execute()) {
  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  for (Certificate certificate : response.handshake().peerCertificates()) {
   System.out.println(CertificatePinner.pin(certificate));
  }
 }
}
origin: square/okhttp

public void run() throws Exception {
 File file = new File("README.md");
 Request request = new Request.Builder()
   .url("https://api.github.com/markdown/raw")
   .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
   .build();
 try (Response response = client.newCall(request).execute()) {
  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  System.out.println(response.body().string());
 }
}
origin: apache/flink

private void validateApiKey() {
  Request r = new Request.Builder().url(validateUrl).get().build();
  try (Response response = client.newCall(r).execute()) {
    if (!response.isSuccessful()) {
      throw new IllegalArgumentException(
        String.format("API key: %s is invalid", apiKey));
    }
  } catch (IOException e) {
    throw new IllegalStateException("Failed contacting Datadog to validate API key", e);
  }
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("https://publicobject.com/helloworld.txt")
   .build();
 Response response = client.newCall(request).execute();
 response.body().close();
}
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();
 final long startNanos = System.nanoTime();
 final Call call = client.newCall(request);
 // Schedule a job to cancel the call in 1 second.
 executor.schedule(() -> {
  System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f);
  call.cancel();
  System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f);
 }, 1, TimeUnit.SECONDS);
 System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
 try (Response response = call.execute()) {
  System.out.printf("%.2f Call was expected to fail, but completed: %s%n",
    (System.nanoTime() - startNanos) / 1e9f, response);
 } catch (IOException e) {
  System.out.printf("%.2f Call failed as expected: %s%n",
    (System.nanoTime() - startNanos) / 1e9f, e);
 }
}
origin: square/okhttp

public void run() throws Exception {
 Request request = new Request.Builder()
   .url("https://api.github.com/gists/c2a7c39532239ff261be")
   .build();
 try (Response response = client.newCall(request).execute()) {
  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  Gist gist = gistJsonAdapter.fromJson(response.body().source());
  for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
   System.out.println(entry.getKey());
   System.out.println(entry.getValue().content);
  }
 }
}
origin: square/okhttp

private @Nullable Response getCacheOnlyResponse(Request request) {
 if (!post && client.cache() != null) {
  try {
   Request cacheRequest = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
   Response cacheResponse = client.newCall(cacheRequest).execute();
   if (cacheResponse.code() != 504) {
    return cacheResponse;
   }
  } catch (IOException ioe) {
   // Failures are ignored as we can fallback to the network
   // and hopefully repopulate the cache.
  }
 }
 return null;
}
origin: square/okhttp

public void run() throws Exception {
 Map<String, String> requestBody = new LinkedHashMap<>();
 requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/");
 RequestBody jsonRequestBody = RequestBody.create(
   MEDIA_TYPE_JSON, mapJsonAdapter.toJson(requestBody));
 Request request = new Request.Builder()
   .url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY)
   .post(jsonRequestBody)
   .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 {
 for (int i = 0; i < 5; i++) {
  System.out.println("    Request: " + i);
  Request request = new Request.Builder()
    .url("https://api.github.com/search/repositories?q=http")
    .build();
  OkHttpClient clientForCall;
  if (i == 2) {
   // Force this request's response to be written to the cache. This way, subsequent responses
   // can be read from the cache.
   System.out.println("Force cache: true");
   clientForCall = client.newBuilder()
     .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
     .build();
  } else {
   System.out.println("Force cache: false");
   clientForCall = client;
  }
  try (Response response = clientForCall.newCall(request).execute()) {
   if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
   System.out.println("    Network: " + (response.networkResponse() != null));
   System.out.println();
  }
 }
}
okhttp3Callexecute

Javadoc

Invokes the request immediately, and blocks until the response can be processed or is in error.

The caller may read the response body with the response's Response#body method. To facilitate connection recycling, callers should always ResponseBody#close().

Note that transport-layer success (receiving a HTTP response code, headers and body) does not necessarily indicate application-layer success: response may still indicate an unhappy HTTP response code like 404 or 500.

Popular methods of Call

  • enqueue
    Schedules the request to be executed at some point in the future.The OkHttpClient#dispatcher defines
  • cancel
    Cancels the request, if possible. Requests that are already complete cannot be canceled.
  • request
    Returns the original request that initiated this call.
  • isCanceled
  • clone
    Create a new, identical call to this one which can be enqueued or executed even if this call has alr
  • isExecuted
    Returns true if this call has been either #execute() or #enqueue(Callback). It is an error to execut
  • timeout
    Returns a timeout that spans the entire call: resolving DNS, connecting, writing the request body, s
  • <init>
  • getResponse
    Performs the request and returns the response. May return null if this call was canceled.
  • getResponseWithInterceptorChain
  • tag
  • tag

Popular in Java

  • Start an intent from android
  • runOnUiThread (Activity)
  • getApplicationContext (Context)
  • putExtra (Intent)
  • Graphics2D (java.awt)
    This Graphics2D class extends the Graphics class to provide more sophisticated control overgraphics
  • FileOutputStream (java.io)
    An output stream that writes bytes to a file. If the output file exists, it can be replaced or appen
  • MalformedURLException (java.net)
    This exception is thrown when a program attempts to create an URL from an incorrect specification.
  • SecureRandom (java.security)
    This class generates cryptographically secure pseudo-random numbers. It is best to invoke SecureRand
  • AtomicInteger (java.util.concurrent.atomic)
    An int value that may be updated atomically. See the java.util.concurrent.atomic package specificati
  • Get (org.apache.hadoop.hbase.client)
    Used to perform Get operations on a single row. To get everything for a row, instantiate a Get objec
  • 21 Best IntelliJ Plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

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