Tabnine Logo
OkHttpClient.newCall
Response.body
Code IndexAdd Tabnine to your IDE (free)

How to use
newCall
method
in
okhttp3.OkHttpClient

Making http requests using okhttp

Refine searchRefine arrow

  • Request.Builder.<init>
  • Call.execute
  • Request.Builder.build
  • Request.Builder.url
  • OkHttpClient.<init>
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 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: yu199195/Raincat

public String post(String url,String json) throws IOException {
  OkHttpClient client = new OkHttpClient();
  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: 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: stackoverflow.com

RequestBody formBody = new FormBody.Builder()
       .add("email", "Jurassic@Park.com")
       .add("tel", "90301171XX")
       .build();
   OkHttpClient client = new OkHttpClient();
   Request request = new Request.Builder()
       .url(url)
       .post(formBody)
       .build();
   Response response = client.newCall(request).execute();
   return response.body().string();
origin: yu199195/Raincat

private String execute(Request request) throws IOException {
  OkHttpClient client = new OkHttpClient();
  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()) {
  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  System.out.println(response.body().string());
 }
}
origin: stackoverflow.com

private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
 RequestBody formBody = new FormEncodingBuilder()
   .add("search", "Jurassic Park")
   .build();
 Request request = new Request.Builder()
   .url("https://en.wikipedia.org/w/index.php")
   .post(formBody)
   .build();
 Response response = client.newCall(request).execute();
 if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
 System.out.println(response.body().string());
}
origin: yu199195/hmily

private <T> T execute(final Request request, final Class<T> classOfT) throws IOException {
  OkHttpClient client = new OkHttpClient();
  Response response = client.newCall(request).execute();
  return GOSN.fromJson(response.body().string(), classOfT);
}
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: stackoverflow.com

private final OkHttpClient client = new OkHttpClient();
  public void run() throws Exception {
   RequestBody formBody = new FormEncodingBuilder()
     .add("email", "Jurassic@Park.com")
     .add("tel", "90301171XX")
     .build();
   Request request = new Request.Builder()
     .url("https://en.wikipedia.org/w/index.php")
     .post(formBody)
     .build();
   Response response = client.newCall(request).execute();
   if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
   System.out.println(response.body().string());
  }
origin: yu199195/myth

private String execute(Request request) throws IOException {
  OkHttpClient client = new OkHttpClient();
  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://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: stackoverflow.com

 private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {
 // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
 RequestBody requestBody = new MultipartBuilder()
   .type(MultipartBuilder.FORM)
   .addPart(
     Headers.of("Content-Disposition", "form-data; name=\"title\""),
     RequestBody.create(null, "Square Logo"))
   .addPart(
     Headers.of("Content-Disposition", "form-data; name=\"image\""),
     RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
   .build();

 Request request = new Request.Builder()
   .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
   .url("https://api.imgur.com/3/image")
   .post(requestBody)
   .build();

 Response response = client.newCall(request).execute();
 if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

 System.out.println(response.body().string());
}
origin: yu199195/Raincat

private <T> T execute(Request request, Class<T> classOfT) throws IOException {
  OkHttpClient client = new OkHttpClient();
  Response response = client.newCall(request).execute();
  return GOSN.fromJson(response.body().string(), classOfT);
}
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: stackoverflow.com

OkHttpClient client = new OkHttpClient();
  Request request = new Request.Builder()
      .url(url)
      .build();
  Response response = client.newCall(request).execute();
  return new JSONArray(response.body().string());
origin: yu199195/Raincat

public <T> T execute(Request request, Type type) throws IOException {
  OkHttpClient client = new OkHttpClient();
  Response response = client.newCall(request).execute();
  return GOSN.fromJson(response.body().string(), type);
}
okhttp3OkHttpClientnewCall

Javadoc

Prepares the request to be executed at some point in the future.

Popular methods of OkHttpClient

  • <init>
  • newBuilder
  • dispatcher
  • connectionPool
  • newWebSocket
    Uses request to connect a new web socket.
  • cache
  • connectTimeoutMillis
    Default connect timeout (in milliseconds).
  • interceptors
    Returns an immutable list of interceptors that observe the full span of each call: from before the c
  • readTimeoutMillis
    Default read timeout (in milliseconds).
  • proxy
  • networkInterceptors
    Returns an immutable list of interceptors that observe a single network request and response. These
  • cookieJar
  • networkInterceptors,
  • cookieJar,
  • sslSocketFactory,
  • writeTimeoutMillis,
  • followRedirects,
  • hostnameVerifier,
  • retryOnConnectionFailure,
  • authenticator,
  • connectionSpecs

Popular in Java

  • Updating database using SQL prepared statement
  • getSystemService (Context)
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • setScale (BigDecimal)
  • BigDecimal (java.math)
    An immutable arbitrary-precision signed decimal.A value is represented by an arbitrary-precision "un
  • Charset (java.nio.charset)
    A charset is a named mapping between Unicode characters and byte sequences. Every Charset can decode
  • Permission (java.security)
    Legacy security code; do not use.
  • Vector (java.util)
    Vector is an implementation of List, backed by an array and synchronized. All optional operations in
  • Filter (javax.servlet)
    A filter is an object that performs filtering tasks on either the request to a resource (a servlet o
  • BoxLayout (javax.swing)
  • Top PhpStorm plugins
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