OkHttp框架加拦截器
2017-10-26 10:15
357 查看
拦截器需要添加依赖:
代码
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){ } } } }); } }
相关文章推荐
- OkHttp框架中拦截器设计精髓之责任链模式
- Okhttp框架搭建及拦截器
- OkHttp框架的RetryAndFollowUpInterceptor请求重定向源码解析
- Retrofit 2.0使用详解,配合OkHttp、Gson,Android最强网络请求框架
- okhttp通过拦截器输出html源码内容
- Okhttp 框架使用和源码分析
- 基于Retrofit、OkHttp、Gson封装通用网络框架
- Android中OKHttp框架的使用
- OkHttp框架的简单使用
- [置顶] 【Android okhttp源码解析 一】okhttp框架简单介绍
- 对主流框架OKHttp的封装,达到一行代码实现一个业务逻辑
- Android项目框架升级尝鲜OkHttp
- OkhttpUtils一个专注于让网络请求更简单的框架
- 详述 Spring MVC 框架中拦截器 Interceptor 的使用方法
- android中Okhttp框架进行网络请求的工具类()
- okhttp公共参数拦截器
- 【菜鸟学框架】——Struts2自定义拦截器
- 【菜鸟学框架】——Struts2自定义拦截器
- OKhttp框架--01
- android 开发 - 使用okhttp框架封装的开发框架