Okhttp 请求前获得请求体的内容

描述:

应用中全局添加网络请求提示自定义Toast,即每个接口请求前显示自定义Dialog, 请求后不管成功还是失败Dialog消失。

初步解决方案:
public class NetInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request().newBuilder().build();
        String url = request.url().toString();
        Response response = null;
        try {
            //请求前显示自定义Dialog
            if (NetValidator.valide(url)) {//有的页面多个接口,或者定时任务的接口不显示,这里通过接口url来判断一下显示不显示的问题
                //当前线程是子线程,Thread[RxCachedThreadScheduler-3,5,main], 需要在主线程显示Dialog
                //将url传到Dialog中,根据不同接口,显示不同的提示语
                AppcationHome.getTopActivity().runOnUiThread(() -> DialogUtils.showNetDialog(url));
            }
            response = chain.proceed(request);
        } finally {
            //请求后不管成功还是失败,Dialog消失, 这里设置一个定时,防止请求过快,Dialog闪一下就消失,甚至不显示的情况。
            //但是定时太长影响用户体验
            Observable.timer(500, TimeUnit.MILLISECONDS)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .subscribe(aLong -> DialogUtils.dismissNetDialog());
        }

        return response;
    }
}
public class NetValidator {
    public static boolean valide(String url) {
        return !url.contains("a") &&
                !url.contains("b") &&
                !url.contains("c");
    }
}
发现问题:

项目中用到Java和Php两种接口,以上针对Java接口是行的通的。
而Php接口规定将请求的数据全部放到请求体中,所有的url都是一样的。
这样便无法通过url来判断显示不显示,及显示什么提示语的逻辑。

此时需要获取请求体中我们定义的标识来判断

进一步解决方案
public class NetInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request().newBuilder().build();
        String url = request.url().toString();
        String phpFunction = getPhpFunction(request, url);
        Response response = null;
        try {
            if (NetValidator.valide(url + phpFunction)) {//php通过phpFunction来判断, java通过url来判断
                AppcationHome.getTopActivity().runOnUiThread(() -> DialogUtils.showNetDialog(url + phpFunction));
            }
            response = chain.proceed(request);
        } finally {
            Observable.timer(500, TimeUnit.MILLISECONDS)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .subscribe(aLong -> DialogUtils.dismissNetDialog());
        }

        return response;
    }

    //如何获得okhttp请求的请求体
    private String getPhpFunction(Request request, String url) {
        String function = "";
        if(url.equals("http://abc.com/apiv2/")) {//php接口地址
            try {
                Buffer sink = new Buffer();
                request.body().writeTo(sink);
                String json = sink.readString(Charset.defaultCharset());
                JSONObject jsonObject = new JSONObject(json);
                function = jsonObject.getString("f");
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return function;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,840评论 0 17
  • 一、简历准备 1、个人技能 (1)自定义控件、UI设计、常用动画特效 自定义控件 ①为什么要自定义控件? Andr...
    lucas777阅读 10,630评论 2 54
  • ERC721是以太坊的非同志资产协议,由于CryptoKitties的火热吸引了注意,以下是ERC721的框架。 ...
    东方泯阅读 9,170评论 2 2
  • 大帅拳霸阅读 1,033评论 0 1
  • 不知道什么原因,那天竟然腿疼的厉害了。是冻的吗?是这几天跑步吗?是走的太多了吗?总之,不知道什么原因,腿疼。上楼疼...
    七月清荷阅读 851评论 0 0