2 ratings
  • 1
  • 2
  • 3
  • 4
  • 5
5 star 2
4 star 0
3 star 0
2 star 0
1 star 0
Compatible with
  • Android

OkHttp v2 2.7.5.0

Xamarin Inc

An HTTP+SPDY client for Android and Java applications.

OkHttp Details

An HTTP & SPDY client for Android and Java applications

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 and SPDY support allows all requests to the same host to share a socket.
  • Connection pooling reduces request latency (if SPDY 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 for services hosted in redundant data centers. OkHttp initiates new connections with modern TLS features (SNI, ALPN), and falls back to TLS 1.0 if the handshake fails.

Using OkHttp is easy. Its 2.0 API is designed with fluent builders and immutability. It supports synchronous blocking calls, async calls with callbacks and async calls using Task with await.

Get a URL

This code downloads a URL and print its contents as a string:

// Create a request
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
    .Url(url)
    .Build();

// Synchronous blocking call
Response response = client.NewCall(request).Execute();
string body = response.Body().String();

Post to a Server

This code posts data to a service:

// Create a request
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.Create(
    MediaType.parse("application/json; charset=utf-8"), 
    "{ 'name': 'Xamarin', 'rating': 5 }");
Request request = new Request.Builder()
    .Url(url)
    .Post(body)
    .Build();

// Synchronous blocking call
Response response = client.NewCall(request).Execute();
string body = response.Body().String();

Asynchronous Requests

This code executes a request on a background thread using a callback mechanism:

OkHttpClient client = ...;
Request request = ...;

// Asynchronous callback
client.NewCall(request).Enqueue(
    response => {
        // Response came back
        string body = response.Body().String();
    }, (request, exception) => {
        // There was an error
    });

This code executes a request on a background thread using Task and await:

OkHttpClient client = ...;
Request request = ...;

// Asynchronous call using Task
Response response = await client.NewCall(request).ExecuteAsync();
string body = response.Body().String();

Release Notes

Version 2.7.4

  • Fix: Don't crash when finding the trust manager if the Play Services (GMS) security provider is installed.
  • Fix: The previous release introduced a performance regression on Android, caused by looking up CA certificates. This is now fixed.

See more release notes

Write a Review

1 review

hu guodong rates this with
  • 1
  • 2
  • 3
  • 4
  • 5

let me see see

Posted on: November 17, 2015 / Version: 2.5.0.0