Samples (Parent)

An HTTP+HTTP/2 client for Android and Java applications

License

License

Categories

Categories

Square Business Logic Libraries Financial
GroupId

GroupId

com.squareup.okhttp3.sample
ArtifactId

ArtifactId

sample-parent
Last Version

Last Version

3.14.9
Release Date

Release Date

Type

Type

pom
Description

Description

Samples (Parent)
An HTTP+HTTP/2 client for Android and Java applications

Download sample-parent

Filename Size
sample-parent-3.14.9.pom 1 KB
Browse

How to add to project

<!-- https://jarcasting.com/artifacts/com.squareup.okhttp3.sample/sample-parent/ -->
<dependency>
    <groupId>com.squareup.okhttp3.sample</groupId>
    <artifactId>sample-parent</artifactId>
    <version>3.14.9</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.squareup.okhttp3.sample/sample-parent/
implementation 'com.squareup.okhttp3.sample:sample-parent:3.14.9'
// https://jarcasting.com/artifacts/com.squareup.okhttp3.sample/sample-parent/
implementation ("com.squareup.okhttp3.sample:sample-parent:3.14.9")
'com.squareup.okhttp3.sample:sample-parent:pom:3.14.9'
<dependency org="com.squareup.okhttp3.sample" name="sample-parent" rev="3.14.9">
  <artifact name="sample-parent" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.squareup.okhttp3.sample', module='sample-parent', version='3.14.9')
)
libraryDependencies += "com.squareup.okhttp3.sample" % "sample-parent" % "3.14.9"
[com.squareup.okhttp3.sample/sample-parent "3.14.9"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

  • guide
  • crawler
  • simple-client
  • slack
  • static-server
  • unixdomainsockets

OkHttp

See the project website for documentation and APIs.

HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.

OkHttp is an HTTP client that’s efficient by default:

  • HTTP/2 support allows all requests to the same host to share a socket.
  • Connection pooling reduces request latency (if HTTP/2 isn’t available).
  • Transparent GZIP shrinks download sizes.
  • Response caching avoids the network completely for repeat requests.

OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and services hosted in redundant data centers. OkHttp supports modern TLS features (TLS 1.3, ALPN, certificate pinning). It can be configured to fall back for broad connectivity.

Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.

Get a URL

This program downloads a URL and prints its contents as a string. Full source.

OkHttpClient client = new OkHttpClient();

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

Post to a Server

This program posts data to a service. Full source.

public static final MediaType JSON
    = MediaType.get("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();
  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}

Further examples are on the OkHttp Recipes page.

Requirements

OkHttp works on Android 5.0+ (API level 21+) and on Java 8+.

OkHttp depends on Okio for high-performance I/O and the Kotlin standard library. Both are small libraries with strong backward-compatibility.

We highly recommend you keep OkHttp up-to-date. As with auto-updating web browsers, staying current with HTTPS clients is an important defense against potential security problems. We track the dynamic TLS ecosystem and adjust OkHttp to improve connectivity and security.

OkHttp uses your platform's built-in TLS implementation. On Java platforms OkHttp also supports Conscrypt, which integrates BoringSSL with Java. OkHttp will use Conscrypt if it is the first security provider:

Security.insertProviderAt(Conscrypt.newProvider(), 1);

The OkHttp 3.12.x branch supports Android 2.3+ (API level 9+) and Java 7+. These platforms lack support for TLS 1.2 and should not be used. But because upgrading is difficult we will backport critical fixes to the 3.12.x branch through December 31, 2021.

Releases

Our change log has release history.

The latest release is available on Maven Central.

implementation("com.squareup.okhttp3:okhttp:4.9.0")

Snapshot builds are available. R8 and ProGuard rules are available.

MockWebServer

OkHttp includes a library for testing HTTP, HTTPS, and HTTP/2 clients.

The latest release is available on Maven Central.

testImplementation("com.squareup.okhttp3:mockwebserver:4.9.0")

GraalVM Native Image

Building your own native images with Graal https://www.graalvm.org/ should work automatically. Please report any bugs or workarounds you find.

See okcurl module for an example build.

$ ./gradlew okcurl:nativeImage
$ ./okcurl/build/graal/okcurl https://httpbin.org/get

License

Copyright 2019 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
com.squareup.okhttp3.sample

Square

Versions

Version
3.14.9
3.14.8
3.14.7
3.14.6
3.14.5
3.14.4
3.14.3
3.14.2
3.14.1
3.14.0
3.13.1
3.13.0
3.12.13
3.12.12
3.12.11
3.12.10
3.12.9
3.12.8
3.12.7
3.12.6
3.12.5
3.12.4
3.12.3
3.12.2
3.12.1
3.12.0
3.11.0
3.10.0
3.9.1
3.9.0
3.8.1
3.8.0
3.7.0
3.6.0
3.5.0
3.4.2
3.4.1
3.4.0
3.4.0-RC1
3.3.1
3.3.0
3.2.0
3.1.2
3.1.1
3.1.0
3.0.1
3.0.0
3.0.0-RC1