OKHttp通信使用(二)
来源:互联网 发布:js获取当前时间加一年 编辑:程序博客网 时间:2024/05/16 12:47
以下代码摘自GitHub:https://github.com/square/okhttp/wiki/Recipes
设置缓存:
OKHTTP使用时通常会先设置缓存大小:
private final OkHttpClient client; public CacheResponse(File cacheDirectory) throws Exception { int cacheSize = 10 * 1024 * 1024; // 10 MiB Cache cache = new Cache(cacheDirectory, cacheSize); client = new OkHttpClient.Builder() .cache(cache) .build(); } public void run() throws Exception { Request request = new Request.Builder() .url("http://publicobject.com/helloworld.txt") .build(); Response response1 = client.newCall(request).execute(); if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1); String response1Body = response1.body().string(); System.out.println("Response 1 response: " + response1); System.out.println("Response 1 cache response: " + response1.cacheResponse()); System.out.println("Response 1 network response: " + response1.networkResponse()); Response response2 = client.newCall(request).execute(); if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2); String response2Body = response2.body().string(); System.out.println("Response 2 response: " + response2); System.out.println("Response 2 cache response: " + response2.cacheResponse()); System.out.println("Response 2 network response: " + response2.networkResponse()); System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body)); }
缓存的持续时间通常在请求头(Header中进行设置)che-Control: max-stale=3600
超时设置:
OKHTTP超时时间设置:
/** * Created by admin on 2016/6/14. */public class TimeOutClass { private OkHttpClient client; public void ConfigureTimeoutss() throws Exception { client = new OkHttpClient.Builder() .connectTimeout(3, TimeUnit.SECONDS) .writeTimeout(3, TimeUnit.SECONDS) .readTimeout(3, TimeUnit.SECONDS) .build(); } public void run() throws Exception { Request request = new Request.Builder() .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay. .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { System.out.println("Response completed: " + response); } }); }}
取消请求:
取消一个请求设置:
public class CanclelingClass { private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { Request request = new Request.Builder() .url("http://httpbin.org/delay/2") // This URL is served with a 2 second delay. .build(); final long startNanos = System.nanoTime(); final Call call = client.newCall(request); // Schedule a job to cancel the call in 1 second. executor.schedule(new Runnable() { @Override public void run() { System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f); call.cancel(); System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f); } }, 1, TimeUnit.SECONDS); try { System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f); Response response = call.execute(); System.out.printf("%.2f Call was expected to fail, but completed: %s%n", (System.nanoTime() - startNanos) / 1e9f, response); } catch (IOException e) { System.out.printf("%.2f Call failed as expected: %s%n", (System.nanoTime() - startNanos) / 1e9f, e); } }}
独立请求设置:
由于在一套程序中只能声明一个OkHttpClient()实例,如果要更改连接超时时间,或者其他参数可使用newBuilder进行设置。
/** * Created by admin on 2016/6/15. */public class PreRequestClass { private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { Request request = new Request.Builder() .url("http://httpbin.org/delay/1") // This URL is served with a 1 second delay. .build(); // Copy to customize OkHttp for this request. OkHttpClient copy = client.newBuilder() .readTimeout(5000, TimeUnit.MILLISECONDS) .build(); copy.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { System.out.println("Response 1 succeeded: " + response); } }); OkHttpClient copy2 = client.newBuilder() .readTimeout(3000, TimeUnit.MILLISECONDS) .build(); copy2.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { System.out.println("Response 2 succeeded: " + response); } }); }}
1 0
- OKHttp通信使用(二)
- OKHttp通信使用(一)
- OKHTTP通信使用(三)HTTPS
- Android OKHttp介绍与使用(二)
- xListView+OKHttp使用(二)
- OkHttp 使用详情二
- JavaEE 使用OKhttp和Action进行通信
- JavaEE 使用OKhttp和Action进行通信
- Okhttp网络通信框架的使用
- OkHttp+ Retrofit使用从0开始(二)
- OkHttp使用(二)加载图片及下载文件
- OkHttp的基本使用(二)Post请求
- OkHttp使用(二)加载图片及下载文件
- OkHttp Wiki翻译(一)使用OkHttp
- OKHttp源码解析(二)
- OKHttp源码解析(二)
- OKHttp源码解析(二)
- OKHttp源码解析(二)
- C语言入门和基本数据类型学习摘要
- 如何提交表单
- HTTP协议使用cookie
- xlistview上拉刷新、下拉加载
- hdu 5686 Problem B【水题】
- OKHttp通信使用(二)
- 定制bootstrap模态框的header
- windows7下安装ruby,rubyGems和devkit
- leetcode-230. Kth Smallest Element in a BST
- js页面跳转整理
- viewpager Scrollview fragment 滑动
- 面试总结CSS HTML
- 射线和三角面的相交判断
- 数据类型、变量和数组