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

How to use
body
method
in
okhttp3.Response

Best Java code snippets using okhttp3.Response.body (Showing top 20 results out of 5,607)

Refine searchRefine arrow

  • OkHttpClient.newCall
  • 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: stackoverflow.com

OkHttpClient client = new OkHttpClient();
 Request request = new Request.Builder()
     .url("http://wwwns.akamai.com/media_resources/globe_emea.png")
     .build();
 client.newCall(request).enqueue(new Callback() {
   @Override
   public void onFailure(Request request, IOException e) {
     System.out.println("request failed: " + e.getMessage());
   }
   @Override
   public void onResponse(Response response) throws IOException {
     response.body().byteStream(); // Read the data from the stream
   }
 });
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: stackoverflow.com

OkHttpClient client = new OkHttpClient();
 RequestBody requestBody = new FormEncodingBuilder()
     .add("grant_type", "authorization_code")
     .add("client_id", "812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5amrf.apps.googleusercontent.com")
     .add("client_secret", "{clientSecret}")
     .add("redirect_uri","")
     .add("code", "4/4-GMMhmHCXhWEzkobqIHGG_EnNYYsAkukHspeYUk9E8")
     .build();
 final Request request = new Request.Builder()
     .url("https://www.googleapis.com/oauth2/v4/token")
     .post(requestBody)
     .build();
 client.newCall(request).enqueue(new Callback() {
   @Override
   public void onFailure(final Request request, final IOException e) {
     Log.e(LOG_TAG, e.toString());                
   }
   @Override
   public void onResponse(Response response) throws IOException {
     try {
       JSONObject jsonObject = new JSONObject(response.body().string());
       final String message = jsonObject.toString(5);
       Log.i(LOG_TAG, message);                    
     } catch (JSONException e) {
       e.printStackTrace();
     }
   }
 });
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/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

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

 @Override
 public Void call(String url) throws Exception {
  okhttp3.Request request = new okhttp3.Request.Builder()
   .post(RequestBody.create(null, new byte[0]))
   .url(url + "?level=" + newLogLevel.name())
   .build();
  try (okhttp3.Response response = new OkHttpClient().newCall(request).execute()) {
   if (response.code() != 200) {
    throw new IOException(
     String.format(
      "Failed to change log level in Compute Engine. Code was '%s' and response was '%s' for url '%s'",
      response.code(),
      response.body().string(),
      url));
   }
   return null;
  }
 }
}
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/myth

/**
 * Post string.
 *
 * @param url  the url
 * @param json the json
 * @return the string
 * @throws IOException the io exception
 */
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

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());
 }
}
okhttp3Responsebody

Javadoc

Returns a non-null value if this response was passed to Callback#onResponse or returned from Call#execute(). Response bodies must be ResponseBody and may be consumed only once.

This always returns null on responses returned from #cacheResponse, #networkResponse, and #priorResponse().

Popular methods of Response

  • code
    Returns the HTTP status code.
  • isSuccessful
    Returns true if the code is in [200..300), which means the request was successfully received, unders
  • headers
  • request
    The wire-level request that initiated this HTTP response. This is not necessarily the same request i
  • newBuilder
  • message
    Returns the HTTP status message.
  • header
  • close
    Closes the response body. Equivalent to body().close().It is an error to close a response that is no
  • protocol
    Returns the HTTP protocol, such as Protocol#HTTP_1_1 or Protocol#HTTP_1_0.
  • networkResponse
    Returns the raw response received from the network. Will be null if this response didn't use the net
  • cacheResponse
    Returns the raw response received from the cache. Will be null if this response didn't use the cache
  • toString
  • cacheResponse,
  • toString,
  • handshake,
  • priorResponse,
  • sentRequestAtMillis,
  • cacheControl,
  • challenges,
  • peekBody,
  • receivedResponseAtMillis

Popular in Java

  • Creating JSON documents from java classes using gson
  • getSystemService (Context)
  • getSharedPreferences (Context)
  • getApplicationContext (Context)
  • Charset (java.nio.charset)
    A charset is a named mapping between Unicode characters and byte sequences. Every Charset can decode
  • Enumeration (java.util)
    A legacy iteration interface.New code should use Iterator instead. Iterator replaces the enumeration
  • Annotation (javassist.bytecode.annotation)
    The annotation structure.An instance of this class is returned bygetAnnotations() in AnnotationsAttr
  • JList (javax.swing)
  • Runner (org.openjdk.jmh.runner)
  • LoggerFactory (org.slf4j)
    The LoggerFactory is a utility class producing Loggers for various logging APIs, most notably for lo
  • Top 12 Jupyter Notebook extensions
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