OkHttp框架加拦截器
来源:互联网 发布:施工计划软件 编辑:程序博客网 时间:2024/06/05 06:09
拦截器需要添加依赖:
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'OKhttp框架依赖compile 'com.squareup.okhttp3:okhttp:3.9.0'
代码
public class OkHttpUtils { //声明客户端 private OkHttpClient client; //防止多个线程同时访问所造成的安全隐患 private volatile static OkHttpUtils utils; private Context context; //子线程 private Handler handler; //构造方法 private OkHttpUtils(Context context){ //拦截器 HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); client=new OkHttpClient.Builder() .addInterceptor(logging) .build(); handler=new Handler(Looper.getMainLooper()); this.context=context; } //单例模式 public static OkHttpUtils getInstance(Context context){ if(utils==null){ //线程同步锁 synchronized (OkHttpUtils.class){ if(utils==null){ utils=new OkHttpUtils(context); utils=utils; } } } return utils; } //网络请求 //syncJsonStringByUrl public void doGet(String url, final FuncJsonString callback){ //网络判断 if (!NetWorkUtil.isNetworkAvailable(context)) { Toast.makeText(context, "没有网络,请查看设置", Toast.LENGTH_SHORT).show(); return; } Request request=new Request.Builder().url(url).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.i("TAG", "解析失败"); } @Override public void onResponse(Call call, Response response) throws IOException { if(response!=null&&response.isSuccessful()){ OnSuccessJsonString(response.body().string(),callback); } } }); } //post提交表单数据 //sendDataForClicent public void dopost(String url, Map<String,String> params, final FuncJsonString callback){ //网络判断 if (!NetWorkUtil.isNetworkAvailable(context)) { Toast.makeText(context, "没有网络,请查看设置", Toast.LENGTH_SHORT).show(); return; } //表单对象,包含input开始的操作 FormBody.Builder from=new FormBody.Builder(); if(params != null && !params.isEmpty()){ for (Map.Entry<String,String> entry:params.entrySet()){ //装载表单值 from.add(entry.getKey(),entry.getValue()); } } RequestBody body=from.build(); //post提交 Request request=new Request.Builder().url(url).post(body).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.i("TAG", "解析失败"); } @Override public void onResponse(Call call, Response response) throws IOException { if(response!=null&&response.isSuccessful()){ OnSuccessJsonString(response.body().string(),callback); } } }); } //返回json字符串的接口 public interface FuncJsonString{ void onResponse(String result); } //请求的返回是json字符串 private void OnSuccessJsonString(final String jsonValue, final FuncJsonString callback){ handler.post(new Runnable() { @Override public void run() { if(callback!=null){ try { callback.onResponse(jsonValue); }catch (Exception e){ } } } }); }}
阅读全文
0 0
- OkHttp框架加拦截器
- Okhttp简单封装加网络拦截器
- OKhttp的简单封装加拦截器
- OkHTTP二次封装加拦截器
- Okhttp框架搭建及拦截器
- Okhttp 拦截器
- okhttp 拦截器调用。
- OkHttp-Interceptors拦截器
- okhttp拦截器+Imageloader
- OkHttp拦截器
- okhttp拦截器
- Okhttp的拦截器
- okhttp应用拦截器
- okhttp拦截器
- Okhttp网络拦截器
- okhttp添加拦截器
- okhttp网络拦截器
- OkHttp拦截器
- MOS管原理用法
- 从oracle官网下载JavaMail所需jar的操作流程
- 第九周项目1—二叉树算法库
- 数据库缓存
- 见之于未萌,识之于未发,网络安全事件应急演练实操指南
- OkHttp框架加拦截器
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- CentOs 6.x 升级 Python 版本
- 第九周 【项目3
- Mybatis模糊查询忽略大小小
- MySQL Group Replication 介绍
- 第七周项目1
- synchronized和lock的区别。
- 二级购物车 (非网络请求)