OkHttp3的使用
来源:互联网 发布:linux口令长度 编辑:程序博客网 时间:2024/05/17 20:22
一、首先需要Gradle,GitHub的链接:http://square.github.io/okhttp/
compile 'com.squareup.okhttp3:okhttp:3.9.0'
二、测试get方法/** * 测试get方法 */@Testpublic 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方法 */@Testpublic 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(); }}四、测试拦截器
/** * 测试拦截器 */@Testpublic 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(); }}五、测试缓存
/** * 测试缓存 */@Testpublic 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(); }}
阅读全文
0 0
- okhttp3的简单使用
- okhttp3的使用
- OkHttp3的使用
- okHttp3的简单使用
- OKhttp3的基本使用
- OkHttp3的使用
- okhttp3基础使用的封装
- Android okhttp3的基本使用
- OkHttp3的使用,代码奉上
- okhttp3版 piccaso的使用
- Android okhttp3的使用详解
- okhttp3基础使用的封装
- Android OKHttp3的初级使用
- OkHttp3网络协议的使用
- OkHTTP3齐全的初级使用
- android Okhttp3的基本使用
- OkHttp3.0的基本使用
- OkHttp3拦截器的使用
- c++之static
- Vue.js实现可编辑的表格
- 解读今年的 Google IO 2017
- SQL查询50例
- easyui 自定义表单内容验证(汉字、字母、数字、邮箱、电话、邮编、身份证号等)
- OkHttp3的使用
- linux远程链接windows
- Chapter 3 基础语法
- jira的发邮件配置
- 实现通用化DLL调用模块的设计解析
- C++学习记录16--VS新版本中iostream库的变化
- SpringBoot--配置Druid连接池
- 海航云聊大数据,解密海航科技集团待遇情况
- mysql 获取varchar类型数字最大值