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

How to use
newCall
method
in
okhttp3.OkHttpClient

Best Java code snippets using okhttp3.OkHttpClient.newCall (Showing top 20 results out of 5,040)

Refine searchRefine arrow

  • Response.body
  • Request.Builder.<init>
  • Request.Builder.url
  • Request.Builder.build
  • Call.execute
  • 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 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: stackoverflow.com

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

OkHttpClient client = new OkHttpClient();

Call post(String url, String json, Callback callback) {
 RequestBody body = RequestBody.create(JSON, json);
 Request request = new Request.Builder()
   .url(url)
   .post(body)
   .build();
 Call call = client.newCall(request);
 call.enqueue(callback);
 return call;
}
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

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: 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: 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: 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: SonarSource/sonarqube

private Response call(String path) throws Exception {
 OkHttpClient client = new OkHttpClient();
 Request request = new Request.Builder()
  .url(jetty.getURI().resolve(path).toString())
  .build();
 return client.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 washingtonPostRequest = new Request.Builder()
   .url("https://www.washingtonpost.com/")
   .build();
 client.newCall(washingtonPostRequest).enqueue(new Callback() {
  @Override public void onFailure(Call call, IOException e) {
  }
  @Override public void onResponse(Call call, Response response) throws IOException {
   try (ResponseBody body = response.body()) {
    // Consume and discard the response body.
    body.source().readByteString();
   }
  }
 });
 Request newYorkTimesRequest = new Request.Builder()
   .url("https://www.nytimes.com/")
   .build();
 client.newCall(newYorkTimesRequest).enqueue(new Callback() {
  @Override public void onFailure(Call call, IOException e) {
  }
  @Override public void onResponse(Call call, Response response) throws IOException {
   try (ResponseBody body = response.body()) {
    // Consume and discard the response body.
    body.source().readByteString();
   }
  }
 });
}
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: yu199195/hmily

/**
 * Post string.
 *
 * @param url  the url
 * @param json the json
 * @return the string
 * @throws IOException the io exception
 */
public String post(final String url, final 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: 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: 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: Exrick/x-boot

/**
 * Base64上传
 * @param data64
 * @return
 */
public String qiniuBase64Upload(String data64) {
  String key = renamePic("");
  // 服务端http://up-z2.qiniup.com 此处存储区域需自行修改
  String url = "http://up-z2.qiniup.com/putb64/-1/key/" + UrlSafeBase64.encodeToString(key);
  RequestBody rb = RequestBody.create(null, data64);
  Request request = new Request.Builder().
      url(url).
      addHeader("Content-Type", "application/octet-stream")
      .addHeader("Authorization", "UpToken " + getUpToken())
      .post(rb).build();
  OkHttpClient client = new OkHttpClient();
  okhttp3.Response response = null;
  try {
    response = client.newCall(request).execute();
  } catch (IOException e) {
    throw new XbootException("上传文件出错,请检查七牛云配置," + e.toString());
  }
  return domain + "/" + key;
}
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
  • getContentResolver (Context)
  • getResourceAsStream (ClassLoader)
  • getSystemService (Context)
  • FileInputStream (java.io)
    An input stream that reads bytes from a file. File file = ...finally if (in != null) in.clos
  • IOException (java.io)
    Signals a general, I/O-related error. Error details may be specified when calling the constructor, a
  • ServletException (javax.servlet)
    Defines a general exception a servlet can throw when it encounters difficulty.
  • HttpServlet (javax.servlet.http)
    Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A sub
  • XPath (javax.xml.xpath)
    XPath provides access to the XPath evaluation environment and expressions. Evaluation of XPath Expr
  • DateTimeFormat (org.joda.time.format)
    Factory that creates instances of DateTimeFormatter from patterns and styles. Datetime formatting i
  • 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