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

How to use
code
method
in
okhttp3.Response

Best Java code snippets using okhttp3.Response.code (Showing top 20 results out of 3,618)

Refine searchRefine arrow

  • Response.body
origin: square/okhttp

@Override public InputStream getInputStream() throws IOException {
 if (!doInput) {
  throw new ProtocolException("This protocol does not support input");
 }
 Response response = getResponse(false);
 if (response.code() >= HTTP_BAD_REQUEST) {
  throw new FileNotFoundException(url.toString());
 }
 return response.body().byteStream();
}
origin: square/okhttp

/**
 * Returns an input stream from the server in the case of error such as the requested file (txt,
 * htm, html) is not found on the remote server.
 */
@Override public InputStream getErrorStream() {
 try {
  Response response = getResponse(true);
  if (HttpHeaders.hasBody(response) && response.code() >= HTTP_BAD_REQUEST) {
   return response.body().byteStream();
  }
  return null;
 } catch (IOException e) {
  return null;
 }
}
origin: google/data-transfer-project

 private String retrieveAuthCode(OkHttpClient client) throws IOException {
  Request.Builder builder = new Request.Builder().url(authRetrievalUrl);

  try (Response authResponse = client.newCall(builder.build()).execute()) {
   ResponseBody authBody = authResponse.body();
   if (authBody == null) {
    throw new AssertionError("AUTH ERROR: " + authResponse.code() + ":" + "<empty body>");
   }
   String authCode = new String(authBody.bytes());

   // System.out.println("AUTH: " + authResponse.code() + ":" + authCode);

   return authCode;
  }
 }
}
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: prestodb/presto

public static <T> JsonResponse<T> execute(JsonCodec<T> codec, OkHttpClient client, Request request)
{
  try (Response response = client.newCall(request).execute()) {
    // TODO: fix in OkHttp: https://github.com/square/okhttp/issues/3111
    if ((response.code() == 307) || (response.code() == 308)) {
      String location = response.header(LOCATION);
      if (location != null) {
        request = request.newBuilder().url(location).build();
        return execute(codec, client, request);
      }
    }
    ResponseBody responseBody = requireNonNull(response.body());
    String body = responseBody.string();
    if (isJson(responseBody.contentType())) {
      return new JsonResponse<>(response.code(), response.message(), response.headers(), body, codec);
    }
    return new JsonResponse<>(response.code(), response.message(), response.headers(), body);
  }
  catch (IOException e) {
    // OkHttp throws this after clearing the interrupt status
    // TODO: remove after updating to Okio 1.15.0+
    if ((e instanceof InterruptedIOException) && "thread interrupted".equals(e.getMessage())) {
      Thread.currentThread().interrupt();
    }
    throw new UncheckedIOException(e);
  }
}
origin: square/okhttp

try (Response response = client.newCall(request).execute()) {
 String responseSource = response.networkResponse() != null ? ("(network: "
   + response.networkResponse().code()
   + " over "
   + response.protocol()
   + ")") : "(cache)";
 int responseCode = response.code();
 Document document = Jsoup.parse(response.body().string(), url.toString());
 for (Element element : document.select("a[href]")) {
  String href = element.attr("href");
origin: bumptech/glide

@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
 responseBody = response.body();
 if (response.isSuccessful()) {
  long contentLength = Preconditions.checkNotNull(responseBody).contentLength();
  stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
  callback.onDataReady(stream);
 } else {
  callback.onLoadFailed(new HttpException(response.message(), response.code()));
 }
}
origin: Alluxio/alluxio

/**
 * Gets object from Qiniu kodo. All requests are authenticated by default,default expires 3600s We
 * use okhttp as our HTTP client and support two main parameters in the external adjustment, MAX
 * request and timeout time.
 * @param key object key
 * @param startPos start index for object
 * @param endPos end index for object
 * @return inputstream
 * @param contentLength object file size
 * @throws IOException
 */
public InputStream getObject(String key, long startPos, long endPos, long contentLength)
  throws IOException {
 String baseUrl = String.format("http://%s/%s", mDownloadHost, key);
 String privateUrl = mAuth.privateDownloadUrl(baseUrl);
 URL url = new URL(privateUrl);
 String objectUrl = String.format("http://%s/%s?%s", mEndPoint, key, url.getQuery());
 Request request = new Request.Builder().url(objectUrl)
   .addHeader("Range",
     "bytes=" + String.valueOf(startPos) + "-"
       + String.valueOf(endPos < contentLength ? endPos - 1 : contentLength - 1))
   .addHeader("Host", mDownloadHost).get().build();
 Response response = mOkHttpClient.newCall(request).execute();
 if (response.code() != 200 && response.code() != 206) {
  throw new IOException(String.format("Qiniu kodo:get object failed errcode:%d,errmsg:%s",
    response.code(), response.message()));
 }
 return response.body().byteStream();
}
origin: Graylog2/graylog2-server

this.lastLastModified.set(ImmutableMap.copyOf(newLastModified));
if (response.body() != null) {
  final String body = response.body().string();
  return Optional.ofNullable(body);
if (response.code() != 304) {
  throw new IOException("Request failed: " + response.message());
origin: TeamNewPipe/NewPipe

private ResponseBody getBody(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException {
  final Request.Builder requestBuilder = new Request.Builder()
      .method("GET", null).url(siteUrl)
      .addHeader("User-Agent", USER_AGENT);
  for (Map.Entry<String, String> header : customProperties.entrySet()) {
    requestBuilder.addHeader(header.getKey(), header.getValue());
  }
  if (!TextUtils.isEmpty(mCookies)) {
    requestBuilder.addHeader("Cookie", mCookies);
  }
  final Request request = requestBuilder.build();
  final Response response = client.newCall(request).execute();
  final ResponseBody body = response.body();
  if (response.code() == 429) {
    throw new ReCaptchaException("reCaptcha Challenge requested");
  }
  if (body == null) {
    response.close();
    return null;
  }
  return body;
}
origin: square/okhttp

private static HttpResponse transformResponse(Response response) {
 int code = response.code();
 String message = response.message();
 BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);
 ResponseBody body = response.body();
 InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength());
 httpResponse.setEntity(entity);
 Headers headers = response.headers();
 for (int i = 0, size = headers.size(); i < size; i++) {
  String name = headers.name(i);
  String value = headers.value(i);
  httpResponse.addHeader(name, value);
  if ("Content-Type".equalsIgnoreCase(name)) {
   entity.setContentType(value);
  } else if ("Content-Encoding".equalsIgnoreCase(name)) {
   entity.setContentEncoding(value);
  }
 }
 return httpResponse;
}
origin: JessYanCoding/MVPArms

@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
  responseBody = response.body();
  if (response.isSuccessful()) {
    long contentLength = Preconditions.checkNotNull(responseBody).contentLength();
    stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
    callback.onDataReady(stream);
  } else {
    callback.onLoadFailed(new HttpException(response.message(), response.code()));
  }
}
origin: pockethub/PocketHub

  throw new IOException("Unexpected response code: " + response.code());
Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
origin: web3j/web3j

@Override
protected InputStream performIO(String request) throws IOException {
  RequestBody requestBody = RequestBody.create(JSON_MEDIA_TYPE, request);
  Headers headers = buildHeaders();
  okhttp3.Request httpRequest = new okhttp3.Request.Builder()
      .url(url)
      .headers(headers)
      .post(requestBody)
      .build();
  okhttp3.Response response = httpClient.newCall(httpRequest).execute();
  ResponseBody responseBody = response.body();
  if (response.isSuccessful()) {
    if (responseBody != null) {
      return buildInputStream(responseBody);
    } else {
      return null;
    }
  } else {
    int code = response.code();
    String text = responseBody == null ? "N/A" : responseBody.string();
    throw new ClientConnectionException("Invalid response received: " + code + "; " + text);
  }
}
origin: square/okhttp

private List<InetAddress> readResponse(String hostname, Response response) throws Exception {
 if (response.cacheResponse() == null && response.protocol() != Protocol.HTTP_2) {
  Platform.get().log(Platform.WARN, "Incorrect protocol: " + response.protocol(), null);
 }
 try {
  if (!response.isSuccessful()) {
   throw new IOException("response: " + response.code() + " " + response.message());
  }
  ResponseBody body = response.body();
  if (body.contentLength() > MAX_RESPONSE_SIZE) {
   throw new IOException("response size exceeds limit ("
     + MAX_RESPONSE_SIZE
     + " bytes): "
     + body.contentLength()
     + " bytes");
  }
  ByteString responseBytes = body.source().readByteString();
  return DnsRecordCodec.decodeAnswers(hostname, responseBytes);
 } finally {
  response.close();
 }
}
origin: SonarSource/sonarqube

private void assertIsPomPomResponse(Response response) throws IOException {
 assertThat(response.code()).isEqualTo(200);
 assertThat(IOUtils.toString(response.body().byteStream())).isEqualTo("ok");
}
origin: amitshekhariitbhu/Fast-Android-Networking

@Override
public void onResponse(Response response) {
  try {
    errorBodyRef.set(response.body().string());
    errorCodeRef.set(response.code());
    latch.countDown();
  } catch (IOException e) {
    assertTrue(false);
  }
}
origin: amitshekhariitbhu/Fast-Android-Networking

@Override
public void onResponse(Response response) {
  try {
    errorBodyRef.set(response.body().string());
    errorCodeRef.set(response.code());
    latch.countDown();
  } catch (IOException e) {
    assertTrue(false);
  }
}
origin: amitshekhariitbhu/Fast-Android-Networking

@Override
public void onResponse(Response response) {
  try {
    errorBodyRef.set(response.body().string());
    errorCodeRef.set(response.code());
    latch.countDown();
  } catch (IOException e) {
    assertTrue(false);
  }
}
origin: Graylog2/graylog2-server

final VersionCheckResponse versionCheckResponse = objectMapper.readValue(response.body().byteStream(), VersionCheckResponse.class);
LOG.error("Version check unsuccessful (response code {}).", response.code());
okhttp3Responsecode

Javadoc

Returns the HTTP status code.

Popular methods of Response

  • body
    Returns a non-null value if this response was passed to Callback#onResponse or returned from Call#ex
  • 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 Sublime Text 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