{"id":19611533,"url":"https://github.com/longforus/interceptorhandleerror","last_synced_at":"2026-06-04T23:31:02.064Z","repository":{"id":130519972,"uuid":"73558562","full_name":"longforus/InterceptorHandleError","owner":"longforus","description":"使用OkHttp的AddInterceptor功能实现RxAndroid+Retrofit请求结果的统一处理","archived":false,"fork":false,"pushed_at":"2016-11-14T05:05:07.000Z","size":97,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-09T10:18:05.941Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/longforus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-12T14:49:45.000Z","updated_at":"2019-11-16T03:20:06.000Z","dependencies_parsed_at":"2023-03-17T23:30:39.434Z","dependency_job_id":null,"html_url":"https://github.com/longforus/InterceptorHandleError","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longforus%2FInterceptorHandleError","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longforus%2FInterceptorHandleError/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longforus%2FInterceptorHandleError/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longforus%2FInterceptorHandleError/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/longforus","download_url":"https://codeload.github.com/longforus/InterceptorHandleError/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240900338,"owners_count":19875532,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T10:40:14.458Z","updated_at":"2025-02-26T17:25:44.535Z","avatar_url":"https://github.com/longforus.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 使用OkHttp的addInterceptor功能实现RxAndroid+Retrofit请求结果的统一处理\n 昨天看了有2个文章:\n\nRetrofit响应数据及异常处理策略:[http://blog.csdn.net/dd864140130/article/details/52689010](http://blog.csdn.net/dd864140130/article/details/52689010)\n\n以及github上另外一种方法实现的demo:\n[https://github.com/ysmintor/Retrofit2RxjavaDemo#retrofit2rxjavademo](https://github.com/ysmintor/Retrofit2RxjavaDemo#retrofit2rxjavademo)\n\n主要就是分离错误逻辑把所有即使返回了结果但是结果非预期的error全部放到onErroe方法中去处理.在拜读了以后,心里有一点小小的疑问,這样虽然分离了error的处理,在实际使用中出错的情况总不会很多吧?多出這么多的跳转处理,性能上的开销会不会大过收益,得不偿失?\n\n 今天又看了一篇文章:\n 关于Retrofit2+Okhttp3实现统一添加请求参数和重定向:\n[http://blog.csdn.net/yyh352091626/article/details/53082350](http://blog.csdn.net/yyh352091626/article/details/53082350)\n\n我就在想能不能使用第三种方法来实现,上面2篇文章所实现的功能呢?\n\n实验了一下发现是可以的,而且好像更加的容易实现:\n\n## 概览\n### 一.定义检测我们需要的error的Interceptor,并添加到OkHttpClient\n\n\tprivate static class ErrorInterceptor implements Interceptor {\n        @Override\n        public Response intercept(Chain chain) throws IOException {\n            Request oldRequest = chain.request();\n            Response response = chain.proceed(oldRequest);\n            byte[] respBytes = response.body()\n                                   .bytes();\n            String respString = new String(respBytes);\n            try {\n                JSONObject object = new JSONObject(respString);\n                int code = (int) object.get(\"total\");//模拟校验情况,实际使用中根据情况来实际判断\n                Log.i(TAG, \"intercept: code = \"+code);//实际返回的code是303  這里模拟使用中返回了结果但是,结果并非完全预期的情况\n                if (code != ResultStatus.OK) {//开启判断制造异常,查看效果\n                    throw new ResultException(\"返回码异常\", code);//抛出自定义异常,在subscriber的onError中被接收,达到分离处理的目的\n                }\n                return response.newBuilder()\n                               .body(ResponseBody.create(null, respBytes))\n                               .build();//在前面获取bytes的时候response的stream已经被关闭了,要重新生成response\n            } catch (JSONException e) {\n                e.printStackTrace();\n                throw new ResultException(\"解析异常\", ResultStatus.PARSE_ERROR);\n            }\n        }\n    }\n\n\n### 二.自定义ResultException,标记非预期情况下要抛出的异常\n\tpublic class ResultException extends RuntimeException {\n   \t private int errorCode;\n\n    public ResultException(String message,int code) {\n        this(message);\n        errorCode = code;\n    }\n\n   \n    public ResultException(Throwable cause) {\n        super(cause);\n    }\n\t}\n\n### 三.在Subscriber的OnError中接收异常并进行处理\n\n \t @Override\n   \t protected void onError(ResultException e) {\n        Log.w(TAG, \"onError: \"+e.getMessage(),e );\n        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT)\n             .show();\n  \t  }\n\n貌似就完成了,简单测试确实可以达到上面的目的,但是是否合用?以及效率问题,我功力太浅,不敢随便乱说,有一个疑问就是在intercept()方法的执行机制,在这个方法里面执行: \n         \n\t  Response response = chain.proceed(oldRequest);\n会不会导致2次请求的发送?如果是這样的话,感觉这个方法就不太可行了.\n经过fiddler抓包分析,发现还是只发出了一个请求,这种方法是可行的 -11月14日\n待研究,待指点....\n\ngithub:\n[https://github.com/longforus/InterceptorHandleError](https://github.com/longforus/InterceptorHandleError)  \n水平有限,没错误不正常,希望得到你的指点\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flongforus%2Finterceptorhandleerror","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flongforus%2Finterceptorhandleerror","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flongforus%2Finterceptorhandleerror/lists"}