OkHttpClient,cache缓存,拦截器
来源:互联网 发布:windows搭建hadoop 编辑:程序博客网 时间:2024/06/11 02:09
导入okhttp依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0'
MainActivity里面public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //get(); // post(); //postString(); //form(); // multipartBody();.... cache();//缓存 //perCall(); } public void get(){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient.Builder() // .addInterceptor(new LoggingInterceptor()) .addNetworkInterceptor(new LoggingInterceptor()) .build(); Request request = new Request.Builder() .url("http://publicobject.com/helloworld.txt") .build(); Call call = client.newCall(request); Response response = call.execute(); if(response.isSuccessful()){ //返回的数据是 android okhttp机器人图案 System.out.println("response = " + response.body().string()); } } catch (Exception e) { e.printStackTrace(); } } }).start(); } //post private void post(){ OkHttpClient client = new OkHttpClient(); String s = Build.BRAND + "/" + Build.MODEL + "/"+Build.VERSION.RELEASE; Request request = new Request.Builder() .url("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0") .addHeader("User-Agent",""+s) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { //输出异常 System.out.println("e ="+e); } @Override public void onResponse(Call call, Response response) throws IOException { //输出 请求到的数据 System.out.println("response.body().string() = " + response.body().string()); } }); } public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); public void postString(){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient(); String postBody = "" + "Releases\n" + "--------\n" + "\n" + " * _1.0_ May 6, 2013\n" + " * _1.1_ June 15, 2013\n" + " * _1.2_ August 11, 2013\n"; Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN,postBody)) .build(); Response response = client.newCall(request).execute(); if(response.isSuccessful()){ //返回一堆 网页的布局 h2 System.out.println(response.body().string()); } } catch (Exception e) { e.printStackTrace(); } } }).start(); } public void form(){ //表单 post请求 携带参数 RequestBody requestBody = new FormBody.Builder() .add("search","Jurassic Park") .build(); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://en.wikipedia.org/w/index.php") .post(requestBody) .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.body().string()); } }); } public static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png"); public void multipartBody() { RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("key","Value") .addFormDataPart("key","filename",RequestBody.create(MEDIA_TYPE_PNG,new File(""))) .build(); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://api.imgur.com/3/image") .post(requestBody) .build(); Call call = client.newCall(request); call.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.body().string()); } }); } public void cache() { new Thread(new Runnable() { @Override public void run() { try { Cache cache = new Cache(getCacheDir(), 10 * 1024 * 1024); OkHttpClient client = new OkHttpClient.Builder() .cache(cache) .build(); Request request = new Request.Builder() .url("http://publicobject.com/helloworld.txt") .cacheControl(CacheControl.FORCE_NETWORK)//强制走网络 .build(); Response response1 = client.newCall(request).execute(); 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(); 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()); } catch (Exception e) { e.printStackTrace(); } } }).start(); } OkHttpClient client = new OkHttpClient(); public void perCall(){ Request request = new Request.Builder() .url("https://en.wikipedia.org/w/index.php") .build(); OkHttpClient okHttpClient1 = client.newBuilder().writeTimeout(10, TimeUnit.SECONDS).build(); OkHttpClient okHttpClient2 = client.newBuilder().writeTimeout(20,TimeUnit.SECONDS).build(); }}
拦截器public class LoggingInterceptor implements Interceptor{ @Override //重写的方法 public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); //logger.info(String.format("Sending request %s on %s%n%s", // request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime(); // logger.info(String.format("Received response for %s in %.1fms%n%s", // response.request().url(), (t2 - t1) / 1e6d, response.headers())); System.out.println("t2 = " + (t2-t1)); return response; }}
阅读全文
1 0
- OkHttpClient,cache缓存,拦截器
- retrofit2.0 OkHttpClient--拦截器
- Android Okhttp缓存:Cache,创建OkHttpClient实现(一)
- OKHttpClient
- OkHttpClient
- OkHttpClient
- 缓存拦截器
- okhttp配置缓存拦截器
- 缓存cache
- 缓存 Cache
- 缓存 Cache
- 缓存--cache
- Cache缓存
- Cache缓存
- Cache缓存
- okhttp之自定义Interceptor:缓存拦截器
- Retrofit2,okhttp3 通过拦截器进行缓存
- Okhttp的缓存机制+拦截器
- JZOJ5466. 【NOIP2017提高A组冲刺11.9】玩游戏
- Android全屏化
- 计算并输出1到n之间的所有数的平方和立方。
- 数据结构实验之二叉树六:哈夫曼编码
- maven的Could not resolve dependencies 的异常以及处理
- OkHttpClient,cache缓存,拦截器
- 杂记
- [Android studio]解决console报错“远程主机强迫关闭了一个连接”
- 3.3
- 习题6.7
- 线程安全的单列模式(4种)
- Eclipse中输出带颜色的log4j日志信息
- 洛谷P1115 最大子段和
- 百度春季校招第2题