mirror of
https://github.com/square/okhttp.git
synced 2025-11-26 06:43:09 +03:00
Some nullability warnings, some diamond operators, adopting Objects.equals(), and some dead code elimination.
229 lines
11 KiB
HTML
229 lines
11 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<title>OkHttp</title>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<meta name="description" content="An HTTP & HTTP/2 client for Android and Java applications">
|
||
<link href="static/bootstrap-combined.min.css" rel="stylesheet">
|
||
<link href="static/app.css" rel="stylesheet">
|
||
<link href="static/app-theme.css" rel="stylesheet">
|
||
<link href="https://fonts.googleapis.com/css?family=Roboto:400,300italic,100,100italic,300" rel="stylesheet" type="text/css">
|
||
<!--[if lt IE 9]><script src="static/html5shiv.min.js"></script><![endif]-->
|
||
</head>
|
||
<body data-target=".content-nav">
|
||
<header>
|
||
<div class="container">
|
||
<div class="row">
|
||
<div class="span5">
|
||
<h1>OkHttp</h1>
|
||
</div>
|
||
<div class="span7">
|
||
<menu>
|
||
<ul>
|
||
<li><a href="#download" class="menu download">Download <span class="version-tag">Latest</span></a></li>
|
||
<li><a href="https://github.com/square/okhttp" data-title="View GitHub Project" class="menu github"><img src="static/icon-github.png" alt="GitHub"/></a></li>
|
||
<li><a href="https://square.github.io/" data-title="Square Open Source Portal" class="menu square"><img src="static/icon-square.png" alt="Square"/></a></li>
|
||
</ul>
|
||
</menu>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
<section id="subtitle">
|
||
<div class="container">
|
||
<div class="row">
|
||
<div class="span12">
|
||
<h2>An <strong>HTTP & HTTP/2</strong> client for Android and Java applications</h2>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="body">
|
||
<div class="container">
|
||
<div class="row">
|
||
<div class="span9">
|
||
<h3 id="overview">Overview</h3>
|
||
<p>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.</p>
|
||
|
||
<p>OkHttp is an HTTP client that’s efficient by default:</p>
|
||
<ul>
|
||
<li>HTTP/2 support allows all requests to the same host to share a socket.</li>
|
||
<li>Connection pooling reduces request latency (if HTTP/2 isn’t available).</li>
|
||
<li>Transparent GZIP shrinks download sizes.</li>
|
||
<li>Response caching avoids the network completely for repeat requests.</li>
|
||
</ul>
|
||
|
||
<p>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 for 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.</p>
|
||
|
||
<p>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.</p>
|
||
|
||
<p>OkHttp supports Android 5.0+ (API level 21+) and Java 8+.</p>
|
||
|
||
<h3 id="examples">Examples</h3>
|
||
<h4>Get a URL</h4>
|
||
<p>This program downloads a URL and prints its contents as a string. <a href="https://raw.github.com/square/okhttp/master/samples/guide/src/main/java/okhttp3/guide/GetExample.java">Full source</a>.
|
||
<pre class="prettyprint">
|
||
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();
|
||
}
|
||
}
|
||
</pre>
|
||
<h4>Post to a Server</h4>
|
||
<p>This program posts data to a service. <a href="https://raw.github.com/square/okhttp/master/samples/guide/src/main/java/okhttp3/guide/PostExample.java">Full source</a>.
|
||
|
||
<pre class="prettyprint">
|
||
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();
|
||
}
|
||
}
|
||
</pre>
|
||
|
||
<h3 id="download">Download</h3>
|
||
<p><a href="https://search.maven.org/remote_content?g=com.squareup.okhttp3&a=okhttp&v=LATEST" class="dl version-href">↓ <span class="version-tag">Latest</span> JAR</a></p>
|
||
<p>You'll also need <a href="https://github.com/square/okio">Okio</a>, which OkHttp
|
||
uses for fast I/O and resizable buffers. Download the
|
||
<a href="https://search.maven.org/remote_content?g=com.squareup.okio&a=okio&v=LATEST">latest JAR</a>.
|
||
<p>The source code to OkHttp, its samples, and this website is <a href="https://github.com/square/okhttp">available on GitHub</a>.</p>
|
||
|
||
<h4>Maven</h4>
|
||
<pre class="prettyprint"><dependency>
|
||
<groupId>com.squareup.okhttp3</groupId>
|
||
<artifactId>okhttp</artifactId>
|
||
<version><span class="version pln"><em>(insert latest version)</em></span></version>
|
||
</dependency></pre>
|
||
|
||
<h4>Gradle</h4>
|
||
<pre class="prettyprint">implementation 'com.squareup.okhttp3:okhttp:<span class="version pln"><em>(insert latest version)</em></span>'</pre>
|
||
|
||
<h3 id="contributing">Contributing</h3>
|
||
<p>If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request.</p>
|
||
<p>When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. Please also make sure your code compiles by running <code>mvn clean verify</code>.</p>
|
||
|
||
<p>Some general advice</p>
|
||
<ul>
|
||
<li>Don’t change public API lightly, avoid if possible, and include your reasoning in the PR if essential. It causes pain for developers who use OkHttp and sometimes runtime errors.</li>
|
||
<li>Favour a working external library if appropriate. There are many examples of OkHttp libraries that can sit on top or hook in via existing APIs.</li>
|
||
<li>Get working code on a personal branch with tests before you submit a PR.</li>
|
||
<li>OkHttp is a small and light dependency. Don't introduce new dependencies or major new functionality.</li>
|
||
<li>OkHttp targets the intersection of RFC correct and widely implemented. Incorrect implementations that are very widely implemented e.g. a bug in Apache, Nginx, Google, Firefox should also be handled.</li>
|
||
</ul>
|
||
|
||
<p>Before your code can be accepted into the project you must also sign the <a href="https://squ.re/sign-the-cla">Individual Contributor License Agreement (CLA)</a>.</p>
|
||
|
||
<h3 id="license">License</h3>
|
||
<pre>Copyright 2016 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.</pre>
|
||
</div>
|
||
<div class="span3">
|
||
<div class="content-nav" data-spy="affix" data-offset-top="80">
|
||
<ul class="nav nav-tabs nav-stacked primary">
|
||
<li><a href="#overview">Overview</a></li>
|
||
<li><a href="#examples">Examples</a></li>
|
||
<li><a href="#download">Download</a></li>
|
||
<li><a href="#contributing">Contributing</a></li>
|
||
<li><a href="#license">License</a></li>
|
||
</ul>
|
||
<ul class="nav nav-pills nav-stacked secondary">
|
||
<li><a href="https://github.com/square/okhttp/wiki">Wiki</a></li>
|
||
<li><a href="3.x/okhttp/">Javadoc</a></li>
|
||
<li><a href="https://stackoverflow.com/questions/tagged/okhttp?sort=active">StackOverflow</a></li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="row">
|
||
<div class="span12 logo">
|
||
<a href="https://squareup.com"><img src="static/logo-square.png" alt="Square, Inc."/></a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
|
||
<script src="static/bootstrap.min.js"></script>
|
||
<script src="static/jquery.smooth-scroll.min.js"></script>
|
||
<script src="static/jquery-maven-artifact.min.js"></script>
|
||
<script src="static/prettify.js"></script>
|
||
<script type="text/javascript">
|
||
$(function() {
|
||
// Syntax highlight code blocks.
|
||
prettyPrint();
|
||
|
||
// Spy on scroll position for real-time updating of current section.
|
||
$('body').scrollspy();
|
||
|
||
// Use smooth-scroll for internal links.
|
||
$('a').smoothScroll();
|
||
|
||
// Enable tooltips on the header nav image items.
|
||
$('.menu').tooltip({
|
||
placement: 'bottom',
|
||
trigger: 'hover',
|
||
container: 'body',
|
||
delay: {
|
||
show: 500,
|
||
hide: 0
|
||
}
|
||
});
|
||
|
||
// Look up the latest version of the library.
|
||
$.fn.artifactVersion({
|
||
'groupId': 'com.squareup.okhttp3',
|
||
'artifactId': 'okhttp'
|
||
}, function(version, url) {
|
||
$('.version').text(version);
|
||
$('.version-tag').text('v' + version);
|
||
$('.version-href').attr('href', url);
|
||
});
|
||
});
|
||
|
||
var _gaq = _gaq || [];
|
||
_gaq.push(['_setAccount', 'UA-40704740-2']);
|
||
_gaq.push(['_trackPageview']);
|
||
|
||
(function() {
|
||
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
||
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
||
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|