okhttp拦截器的实现
来源:互联网 发布:unity3d卡牌游戏源码 编辑:程序博客网 时间:2024/06/05 09:38
public class LoggingInterceptor implements okhttp3.Interceptor { public static String TAG = "LogInterceptor"; @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long startTime = System.currentTimeMillis(); Response response = chain.proceed(chain.request()); long endTime = System.currentTimeMillis(); long duration=endTime-startTime; MediaType mediaType = response.body().contentType(); String content = response.body().string(); Log.d(TAG,"\n"); Log.d(TAG,"----------Start----------------"); Log.d(TAG, "| "+request.toString()); String method=request.method(); if("POST".equals(method)){ StringBuilder sb = new StringBuilder(); if (request.body() instanceof FormBody) { FormBody body = (FormBody) request.body(); for (int i = 0; i < body.size(); i++) { sb.append(body.encodedName(i) + "=" + body.encodedValue(i) + ","); } sb.delete(sb.length() - 1, sb.length()); Log.d(TAG, "| RequestParams:{"+sb.toString()+"}"); } } Log.d(TAG, "| Response:" + content); Log.d(TAG,"----------End:"+duration+"毫秒----------"); return response.newBuilder() .body(ResponseBody.create(mediaType, content)) .build(); }}
public class NetRequestData { public static void call(String url, Map<String,String> params, final Callbak callbak){ OkHttpClient client=new OkHttpClient.Builder() .addInterceptor(new LoggingInterceptor()) .build(); FormBody.Builder body= new FormBody.Builder(); for (Map.Entry<String, String> stringStringEntry : params.entrySet()) { body.add(stringStringEntry.getKey(),stringStringEntry.getValue()); } RequestBody build = body.build(); Request request=new Request.Builder().url(url).header("User-Agent", "OkHttp Example").post(build).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { callbak.RequestFaliure(call,e); } @Override public void onResponse(Call call, Response response) throws IOException { callbak.RequestSuccess(call,response); } }); }
阅读全文
0 0
- okhttp拦截器的实现
- okhttp拦截器的实现
- Okhttp的拦截器
- okhttp拦截器的使用
- 关于OKhttp的拦截器
- OkHttp的拦截器Interceptor
- okHttp拦截器的使用
- OkHttp的请求拦截器,实现请求拦截,打印日志到logcat
- OKHTTP的拦截器以及https访问
- okhttp的interceptor拦截器功能
- 一起来写OKHttp的拦截器
- 浅析 OkHttp 的拦截器机制
- OKHTTP拦截器的简单使用
- OkHttp拦截器的简单使用
- OkHttp的拦截器机制分析
- Android Okhttp的网络拦截器
- OKhttp的简单封装加拦截器
- okHttp的封装和拦截器
- wps for linux显示系统缺失字体解决办法
- okhttp封装+拦截器+异常捕获类
- Android数据存储之Sqlite的介绍及使用
- hdu 3605 二分图多重匹配
- shader实例(五)GrabPass捕捉屏幕纹理
- okhttp拦截器的实现
- Hadoop学习笔记(六)实战word count
- OKHTTP3的使用与封装
- zookeeper安装
- 如何从硬盘直接引导 Linux ISO映像
- 具备C语言的能力下,快速学会使用AWK为了做些什么
- servlet 读写文件的坑
- 从CSV读取数字作为字符串
- 学习ssm框架——Mybatis版本问题解决方案(一)