初识网络请求框架--OKHttp官方介绍

OKHttp

官网:http://square.github.io/okhttp/

GitHub: https://github.com/square/okhttp

首先看官方介绍:

Overview

HTTP是现代网络应用程序的方式。 这是我们如何与媒体交换数据。 做HTTP有效地使你的东西加载更快,节省带宽。 


OkHttp默认是一个有效的HTTP客户端:

HTTP / 2支持允许所有请求相同的主机共享一个套接字。

连接池可以减少请求延迟(如果HTTP / 2不可使用)。

透明的GZIP收缩下载大小。

响应缓存避免了网络完全重复请求。 


OkHttp坚守网络时麻烦:它会悄然从常见的连接问题中恢复过来。 如果你的服务有多个IP地址OkHttp将尝试备用地址如果第一个连接失败。 这是必要的IPv4 + IPv6和服务驻留在冗余的数据中心。 与现代TLS OkHttp发起新连接特性(SNI ALPN),并落回TLS 1.0如果握手失败。  


使用OkHttp很容易。 它的请求/响应API设计流畅建筑商和不变性。 它同时支持同步阻塞调用和异步调用回调。

OkHttp支持安卓2.3及以上。 对于Java,最低要求是1.7。 


Examples

GET A URL

这个程序下载URL和打印内容为字符串。 

package okhttp3.guide;

import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GetExample {
  OkHttpClient client = new OkHttpClient();

  String run(String url) throws IOException {
    Request request = new Request.Builder()
        .url(url)
        .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
  }

  public static void main(String[] args) throws IOException {
    GetExample example = new GetExample();
    String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
    System.out.println(response);
  }
}
POST TO A SERVER

这个程序是向服务器抛一个数据。

package okhttp3.guide;

import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class PostExample {
  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();
  }

  String bowlingJson(String player1, String player2) {
    return "{'winCondition':'HIGH_SCORE',"
        + "'name':'Bowling',"
        + "'round':4,"
        + "'lastSaved':1367702411696,"
        + "'dateStarted':1367702378785,"
        + "'players':["
        + "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39},"
        + "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}"
        + "]}";
  }

  public static void main(String[] args) throws IOException {
    PostExample example = new PostExample();
    String json = example.bowlingJson("Jesse", "Jake");
    String response = example.post("http://www.roundsapp.com/post", json);
    System.out.println(response);
  }
}

Download

你还需要Okio,OkHttp用途快速I / O和可调整大小的缓冲区。 下载最新的JAR。

OkHttp源代码,其样本,这个网站可以在GitHub上。 




转载于:https://my.oschina.net/whhos/blog/669910

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值