{"id":5025,"date":"2021-10-08T09:49:42","date_gmt":"2021-10-08T01:49:42","guid":{"rendered":"http:\/\/122.152.205.50\/wordpress\/?p=5025"},"modified":"2021-11-02T16:51:18","modified_gmt":"2021-11-02T08:51:18","slug":"retrofit%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90-%e8%8d%89%e7%a8%bf","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=5025","title":{"rendered":"Retrofit2.9.0\u6e90\u7801\u5206\u6790(Github)"},"content":{"rendered":"<p>\u53c2\u8003\u8d44\u6599\uff1a<br \/>\nretrofit\u6ce8\u89e3\u5927\u5168\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.jianshu.com\/p\/e877b2f02966\">https:\/\/www.jianshu.com\/p\/e877b2f02966<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/wan_dou_\/article\/details\/116067728\">https:\/\/blog.csdn.net\/wan_dou_\/article\/details\/116067728<\/a> (\u6e90\u7801\u5206\u6790)<\/p>\n<h3>\u4e00\u3001\u521b\u5efaRetrofit\u5b9e\u4f8b<\/h3>\n<p>\u901a\u8fc7\u6784\u9020\u8005\u8bbe\u8ba1\u6a21\u5f0f\u6784\u5efaretrofit\u5b9e\u4f8b<\/p>\n<h3>\u4e8c\u3001\u521b\u5efaService\uff08\u63a5\u53e3\uff09\u5b9e\u4f8b<\/h3>\n<pre><code class=\"language-java line-numbers\">public &lt;T&gt; T create(final Class&lt;T&gt; service) {\n    \/\/\u68c0\u6d4bservice\u5b9a\u4e49\u7684\u65b9\u6cd5\u7684\u5408\u6cd5\u6027\n    \/\/\u5c06service\u4e2d\u7684\u65b9\u6cd5\u53cd\u5c04\u7f13\u5b58\u8d77\u6765\n    validateServiceInterface(service);\n\n    \/\/\u8fd4\u56de\u52a8\u6001\u4ee3\u7406\u5b9e\u4f8b\n    return (T)\n        Proxy.newProxyInstance(\n            service.getClassLoader(),\n            new Class&lt;?&gt;[] {service},\n            new InvocationHandler() {\n              private final Object[] emptyArgs = new Object[0];\n\n              @Override\n              public @Nullable Object invoke(Object proxy, Method method, @Nullable Object[] args)\n                  throws Throwable {\n                \/\/ If the method is a method from Object then defer to normal invocation.\n                if (method.getDeclaringClass() == Object.class) {\n                  return method.invoke(this, args);\n                }\n                args = args != null ? args : emptyArgs;\n                Platform platform = Platform.get();\n                return platform.isDefaultMethod(method)\n                    ? platform.invokeDefaultMethod(method, service, proxy, args)\n                    : loadServiceMethod(method).invoke(args);\n              }\n            });\n  }\n<\/code><\/pre>\n<ul>\n<li>validateServiceInterface<\/li>\n<\/ul>\n<p>-> loadServiceMethod<br \/>\n\u5b58\u50a8<code>Map&lt;Method, ServiceMethod&lt;?&gt;&gt;<\/code>,\u5c06\u53cd\u5c04\u7684\u7ed3\u679c\u4fdd\u5b58\u8d77\u6765\uff0c\u907f\u514d\u91cd\u590d\u53cd\u5c04\uff0c\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<p><strong>ServiceMethod<\/strong><br \/>\n-> parseAnnotations<\/p>\n<p>|---- <strong>RequestFactory<\/strong><br \/>\n-> Builder\u6784\u9020<\/p>\n<pre><code class=\"language-java line-numbers\"> Builder(Retrofit retrofit, Method method) {\n      this.retrofit = retrofit;\n      this.method = method;\n      \/\/\u53cd\u5c04\u65b9\u6cd5\u7684\u6ce8\u89e3\n      this.methodAnnotations = method.getAnnotations();\n      \/\/\u53cd\u5c04\u65b9\u6cd5\u7684\u53c2\u6570\n      this.parameterTypes = method.getGenericParameterTypes();\n      \/\/\u53cd\u5c04\u65b9\u6cd5\u53c2\u6570\u7684\u6ce8\u89e3\n      this.parameterAnnotationsArray = method.getParameterAnnotations();\n    }\n<\/code><\/pre>\n<p>-> parseMethodAnnotation<br \/>\n\u89e3\u6790HTTP\u8bf7\u6c42\u65b9\u6cd5\u4e0a\u7684\u5404\u79cd\u6ce8\u89e3\uff1a\u8bf7\u6c42\u65b9\u6cd5\u6ce8\u89e3@GET\u7b49\u3001\u8bf7\u6c42\u5934\u6ce8\u89e3@Headers\u3001\u6807\u8bb0\u6ce8\u89e3@Multipart\u3001@FormUrlEncoded<br \/>\n<img src=\"http:\/\/122.152.205.50\/wordpress\/wp-content\/uploads\/2021\/10\/\u65b9\u6cd5\u6ce8\u89e3\u89e3\u6790.png\" alt=\"\" \/><\/p>\n<blockquote><p>\n  1\uff09\u4e00\u4e2a\u65b9\u6cd5\u53ea\u80fd\u6709\u4e00\u79cdHTTP\u6ce8\u89e3<br \/>\n  2\uff09\u52a8\u6001\u67e5\u8be2\u53c2\u6570\u5fc5\u9700\u4f7f\u7528@Query\u6ce8\u89e3<br \/>\n  3\uff09FormUrlEncoded\u4e0eMultipart\u6ce8\u89e3\u4e92\u65a5\uff0c\u4e14\u53ea\u80fd\u7528\u4e8e\u5e26\u8bf7\u6c42\u4f53\u7684\u8bf7\u6c42\u65b9\u6cd5<br \/>\n  4) \u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u4e0d\u80fd\u662fvoid\n<\/p><\/blockquote>\n<p>-> parseParameter<br \/>\n\u89e3\u6790\u65b9\u6cd5\u53c2\u6570<br \/>\n    ->->parseParameterAnnotation<br \/>\n    \u89e3\u6790\u65b9\u6cd5\u6ce8\u89e3<br \/>\n    \u5404\u79cd\u65b9\u6cd5\u53c2\u6570\u7684\u6ce8\u89e3\u7684\u62a5\u9519\u65e5\u5fd7\u5c31\u662f\u8fd9\u91cc\u6253\u5370\u7684<\/p>\n<p>->-> parseHttpMethodAndPath<br \/>\n\u2003\u2003\u8bf7\u6c42\u65b9\u6cd5\u53ca\u8bf7\u6c42url\u89e3\u6790<br \/>\n->-> parseHeaders<br \/>\n\u2003\u2003contentType\u89e3\u6790<\/p>\n<p>|---- <strong>HttpServiceMethod<\/strong><br \/>\n-> parseAnnotations<br \/>\ncreateCallAdapter\uff0c\u521b\u5efaCallAdapter\u5bf9\u8c61<br \/>\ncreateResponseConverter\uff0c\u521b\u5efaConverter\u5bf9\u8c61<br \/>\n\u521b\u5efa\u5e76\u8fd4\u56deHttpServiceMethod<br \/>\n- Proxy.newProxyInstance<br \/>\n\u8fd4\u56de\u52a8\u6001\u4ee3\u7406\u521b\u5efa\u7684\u63a5\u53e3\u5b9e\u4f8b<\/p>\n<pre><code class=\"language-java line-numbers\">  Platform platform = Platform.get();\n            \/\/\u83b7\u53d6Platform\uff0c\u5224\u65ad\u65b9\u6cd5\u662f\u5426\u662f\u63a5\u53e3\u7684\u9ed8\u8ba4\u65b9\u6cd5\u3002\n            return platform.isDefaultMethod(method)\n              ? platform.invokeDefaultMethod(method, service, proxy, args)\n              : loadServiceMethod(method).invoke(args);\n<\/code><\/pre>\n<h3>\u4e09\u3001\u53d1\u8d77\u8bf7\u6c42<\/h3>\n<p>\u6240\u6709\u7684\u63a5\u53e3\u53d1\u8d77\u8bf7\u6c42\uff0c\u6700\u7ec8\u90fd\u662f\u901a\u8fc7\u52a8\u6001\u4ee3\u7406\u8d70<code>loadServiceMethod(method).invoke(args)<\/code>\u7684\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662fHttpServiceMethod\u7684invoke\u65b9\u6cd5\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u770b\u61c2\u8fd9\u5757\u7684\u4ee3\u7801\uff0c\u4f60\u9700\u8981\u61c2<strong>kotlin\u534f\u7a0b<\/strong>\u3002<br \/>\n\u53c2\u8003\u4e00\u4e0b\u535a\u6587<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.jianshu.com\/p\/c9f123c21d82\/\">https:\/\/www.jianshu.com\/p\/c9f123c21d82\/<\/a><\/p>\n<pre><code class=\"language-java line-numbers\"> CallAdapter&lt;ResponseT, ReturnT&gt; callAdapter =\n        createCallAdapter();\n\n Converter&lt;ResponseBody, ResponseT&gt; responseConverter =\n        createResponseConverter();\n\n\/\/\u4e0d\u662f\u6302\u8d77\u65b9\u6cd5\n if (!isKotlinSuspendFunction) {\n      return new CallAdapted&lt;&gt;();\n    } else if (continuationWantsResponse) {\n      \/\/noinspection unchecked Kotlin compiler guarantees ReturnT to be Object.\n      return (HttpServiceMethod&lt;ResponseT, ReturnT&gt;)\n          new SuspendForResponse&lt;&gt;();\n    } else {\n      \/\/noinspection unchecked Kotlin compiler guarantees ReturnT to be Object.\n      return (HttpServiceMethod&lt;ResponseT, ReturnT&gt;)\n          new SuspendForBody&lt;&gt;();\n    }\n<\/code><\/pre>\n<p>\/\/TODO \u672a\u5b8c\u5f85\u7eed...<\/p>\n<button class=\"simplefavorite-button\" data-postid=\"5025\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u8d44\u6599\uff1a retrofit\u6ce8\u89e3\u5927\u5168\uff1ahttps:\/\/www.jianshu.com\/p\/e877b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[266],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5025"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5025"}],"version-history":[{"count":18,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5025\/revisions"}],"predecessor-version":[{"id":5284,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5025\/revisions\/5284"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5025"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}