OkHttp3使用

一,使用原理介绍
Okhttp特点概述,来自官网概述:

HTTP是目前网络应用程序交互的方式。它让我们如何与媒体交换数据。使用HTTP能够有效的节省资源,加载更快。

OkHttp作为HTTP使用的特点,具体如下:

  1. HTTP / 2支持允许所有请求相同的主机共享一个套接字。
  2. 连接池可以减少请求延迟(如果HTTP / 2不可使用)。
  3. 透明的GZIP处理降低了下载数据的大小。
  4. 响应缓存避免了网络完全重复请求。
 OkHttp官网地址:http://square.github.io/okhttp
 OkHttp GitHub地址:https://github.com/square/okhttp

用法介绍:

(1)GET请求

package dome.test.com.demo;

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

(2)POST请求

package dome.test.com.demo;

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");
        System.out.println(json);
        String response = example.post("http://www.roundsapp.com/post", json);
        System.out.println(response);
    }
}

(3)在android studio的使用步骤:

compile 'com.squareup.okhttp3:okhttp:3.4.2'

官网api:
http://square.github.io/okhttp/3.x/okhttp/


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值