OkHttp3的使用

一、首先需要Gradle,GitHub的链接:http://square.github.io/okhttp/

compile 'com.squareup.okhttp3:okhttp:3.9.0'
二、测试get方法

/**
 * 测试get方法
 */
@Test
public void testGet() {
    //创建OKHttpClient对象
    OkHttpClient okHttpClient = new OkHttpClient();
    //创建request对象
    Request request = new Request.Builder()
            .url("http://httpbin.org/get?id=id")
            .build();
    try {
        Response response = okHttpClient.newCall(request).execute();
        System.out.print(response.body().string());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
三、测试post方法

/**
 * 测试post方法
 */
@Test
public void testPost() {
    //创建OKHttpClient对象
    OkHttpClient okHttpClient = new OkHttpClient();
    //创建request对象
    MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
    RequestBody body = RequestBody.create(mediaType, "{\"name\",\"东哥\"}");
    Request request = new Request.Builder()
            .url("http://httpbin.org/post")
            .post(body)
            .build();
    try {
        Response response = okHttpClient.newCall(request).execute();
        System.out.print(response.body().string());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
四、测试拦截器

/**
 * 测试拦截器
 */
@Test
public void testInterceptor() {
    //定义一个拦截器
    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            long start = System.currentTimeMillis();
            Request request = chain.request();
            Response response = chain.proceed(request);//继续向下传递
            long end = System.currentTimeMillis();
            System.out.print("cost time:" + (end - start));
            return response;
        }
    };
    //创建OKHttpClient对象
    OkHttpClient okHttpClient = new OkHttpClient
            .Builder()
            .addInterceptor(interceptor)
            .build();
    //创建request对象
    Request request = new Request.Builder()
            .url("http://httpbin.org/get?id=id")
            .build();
    try {
        Response response = okHttpClient.newCall(request).execute();
        System.out.print(response.body().string());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
五、测试缓存

/**
 * 测试缓存
 */
@Test
public void testCache() {
    //创建缓存对象
    Cache cache = new Cache(new File("cache.cache"), 1024 * 1024);
    //创建OKHttpClient对象
    OkHttpClient okHttpClient = new OkHttpClient.Builder().cache(cache).build();
    //创建request对象
    Request request = new Request.Builder()
            .url("http://httpbin.org/get?id=id")
            //.cacheControl(CacheControl.FORCE_CACHE)强制为缓存
            .build();
    try {
        Response response = okHttpClient.newCall(request).execute();
        Response cacheResponse = response.cacheResponse();
        Response networkResponse = response.networkResponse();
        if (cacheResponse != null) {
            System.out.print("来自缓存");
        } else if (networkResponse != null) {
            System.out.print("来自网络");
        }
        System.out.print(response.body().string());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值