{"id":1406,"date":"2021-02-19T10:42:54","date_gmt":"2021-02-19T02:42:54","guid":{"rendered":"http:\/\/wthee.xyz\/?p=1406"},"modified":"2021-02-19T10:42:54","modified_gmt":"2021-02-19T02:42:54","slug":"android-retrofit-%e8%bf%9b%e8%a1%8c-https-%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/wthee.xyz\/index.php\/2021\/02\/19\/android-retrofit-%e8%bf%9b%e8%a1%8c-https-%e8%af%b7%e6%b1%82\/","title":{"rendered":"Android Retrofit \u8fdb\u884c https \u8bf7\u6c42"},"content":{"rendered":"<h4>\u7533\u8bf7\u8bc1\u4e66<\/h4>\n<ul>\n<li><a href=\"https:\/\/letsencrypt.osfipin.com\/\"><strong>Let\u2019s Encrypt<\/strong><\/a>:\u514d\u8d39\uff0c\u6709\u6548\u671f\u4e09\u4e2a\u6708<\/li>\n<\/ul>\n<h4>\u914d\u7f6e<\/h4>\n<ul>\n<li>\u628a certificate.crt \u6587\u4ef6\u653e\u5230 \/res\/raw \u8def\u5f84\u4e0b<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/wthee.xyz\/wp-content\/uploads\/2021\/02\/05B3CFDA-D388-40c5-BFDB-8EDB0D54A9D5.png?w=1170&#038;ssl=1\" alt=\"image-20210219103425018\" data-recalc-dims=\"1\" \/><\/p>\n<ul>\n<li>\u4ee3\u7801<\/li>\n<\/ul>\n<pre><code class=\"\" data-line=\"\">...\nimport okhttp3.OkHttpClient\nimport retrofit2.Retrofit\nimport retrofit2.converter.gson.GsonConverterFactory\nimport java.security.KeyStore\nimport java.security.cert.CertificateFactory\nimport javax.net.ssl.SSLContext\nimport javax.net.ssl.TrustManagerFactory\nimport javax.net.ssl.X509TrustManager\n...\n\n\/**\n * \u52a0\u8f7d SSL \u8bc1\u4e66\n *\/\nprivate fun initSSL(): List&lt;Any&gt; {\n    val cf = CertificateFactory.getInstance(&quot;X.509&quot;)\n    val inputStream = context.resources.openRawResource(R.raw.certificate)\n    val ca = cf.generateCertificate(inputStream)\n    val kStore = KeyStore.getInstance(KeyStore.getDefaultType())\n    kStore.load(null, null)\n    kStore.setCertificateEntry(&quot;ca&quot;, ca)\n    val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())\n    tmf.init(kStore)\n    val sslContext = SSLContext.getInstance(&quot;TLS&quot;)\n    sslContext.init(null, tmf.trustManagers, null)\n    return listOf(sslContext, tmf.trustManagers[0])\n}\n\n\/**\n * \u521b\u5efa [OkHttpClient]\n *\/\nfun getClient(): OkHttpClient {\n    val params = initSSL()\n    return OkHttpClient.Builder()\n        .sslSocketFactory(\n            (params[0] as SSLContext).socketFactory,\n            params[1] as X509TrustManager\n        )\n        .build()\n}\n\n\/**\n * \u521b\u5efa\u670d\u52a1\n *\/\nfun &lt;T&gt; create(serviceClass: Class&lt;T&gt;, url: String): T {\n    val builder = Retrofit.Builder()\n        .baseUrl(url)\n        .addConverterFactory(GsonConverterFactory.create())\n        .client(getClient())\n\n    return builder.build().create(serviceClass)\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Retrofit 2 \u4f7f\u7528\u8bc1\u4e66\u8fdb\u884c https \u8bf7\u6c42\u7684\u4ee3\u7801\u5b9e\u73b0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"default","_kad_post_title":"default","_kad_post_layout":"default","_kad_post_sidebar_id":"","_kad_post_content_style":"default","_kad_post_vertical_padding":"default","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_jetpack_newsletter_access":""},"categories":[3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pcwHG0-mG","jetpack-related-posts":[{"id":1579,"url":"https:\/\/wthee.xyz\/index.php\/2021\/06\/09\/jetpack-compose-navigation-%e7%9a%84%e4%bd%bf%e7%94%a8\/","url_meta":{"origin":1406,"position":0},"title":"Jetpack Compose Navigation \u7684\u4f7f\u7528","author":"wthee","date":"2021\u5e746\u67089\u65e5","format":false,"excerpt":"Compose \u7248\u672c\uff1a1.0.0-beta08 Navigation \u7248\u672c\uff1a2.4.0-alpha0\u2026","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":84,"url":"https:\/\/wthee.xyz\/index.php\/2020\/12\/15\/android-recyclerview-%e5%88%86%e7%bb%84%e6%98%be%e7%a4%ba\/","url_meta":{"origin":1406,"position":1},"title":"Android RecyclerView \u5206\u7ec4\u663e\u793a","author":"wthee","date":"2020\u5e7412\u670815\u65e5","format":false,"excerpt":"RecyclerView \u5206\u7ec4\u6548\u679c\u7684\u5b9e\u73b0\u65b9\u5f0f","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":89,"url":"https:\/\/wthee.xyz\/index.php\/2020\/12\/22\/android-navigation-%e6%b7%bb%e5%8a%a0%e8%bf%87%e6%b8%a1%e6%95%88%e6%9e%9c\/","url_meta":{"origin":1406,"position":2},"title":"Android Navigation \u6dfb\u52a0\u8fc7\u6e21\u6548\u679c","author":"wthee","date":"2020\u5e7412\u670822\u65e5","format":false,"excerpt":"\u4f7f\u7528 Container transform\u4e3a\u5bfc\u822a\u7ec4\u4ef6 Navigation \u6dfb\u52a0\u8fc7\u6e21\u52a8\u753b","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1555,"url":"https:\/\/wthee.xyz\/index.php\/2021\/05\/31\/jetpack-compose-%e5%85%a5%e9%97%a8%e5%bf%85%e7%9c%8b%e7%9a%84%e6%96%87%e6%a1%a3-%e9%a1%b9%e7%9b%ae\/","url_meta":{"origin":1406,"position":3},"title":"Jetpack Compose \u5165\u95e8\u5fc5\u770b\u7684\u6587\u6863\/\u9879\u76ee","author":"wthee","date":"2021\u5e745\u670831\u65e5","format":false,"excerpt":"Jetpack Compose \u76f8\u5173\u7684\u4e00\u4e9b\u6587\u6863\/\u9879\u76ee\u6c47\u603b","rel":"","context":"\u5728\u201cJetpack Compose\u201d\u4e2d","block_context":{"text":"Jetpack Compose","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/jetpack-compose\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/wthee.xyz\/wp-content\/uploads\/2021\/05\/jetpack_compose.png?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/wthee.xyz\/wp-content\/uploads\/2021\/05\/jetpack_compose.png?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/wthee.xyz\/wp-content\/uploads\/2021\/05\/jetpack_compose.png?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/wthee.xyz\/wp-content\/uploads\/2021\/05\/jetpack_compose.png?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/wthee.xyz\/wp-content\/uploads\/2021\/05\/jetpack_compose.png?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":1573,"url":"https:\/\/wthee.xyz\/index.php\/2021\/06\/08\/jetpack-compose-%e5%ae%9e%e7%8e%b0%e6%bb%91%e5%8a%a8%e6%8a%98%e5%8f%a0%e6%95%88%e6%9e%9c\/","url_meta":{"origin":1406,"position":4},"title":"Jetpack Compose \u5b9e\u73b0\u6ed1\u52a8\u6298\u53e0\u6548\u679c","author":"wthee","date":"2021\u5e746\u67088\u65e5","format":false,"excerpt":"\u5229\u7528 rememberScrollState() \uff0c\u5b9e\u73b0\u6ed1\u52a8\u6298\u53e0\u7684\u6548\u679c","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1563,"url":"https:\/\/wthee.xyz\/index.php\/2021\/06\/08\/jetpack-compose-chipgroup\/","url_meta":{"origin":1406,"position":5},"title":"Jetpack Compose ChipGroup","author":"wthee","date":"2021\u5e746\u67088\u65e5","format":false,"excerpt":"\u5b9e\u73b0 ChipGroup \u7ec4\u4ef6","rel":"","context":"\u5728\u201cAndroid \u5f00\u53d1\u201d\u4e2d","block_context":{"text":"Android \u5f00\u53d1","link":"https:\/\/wthee.xyz\/index.php\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1406"}],"collection":[{"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=1406"}],"version-history":[{"count":2,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1406\/revisions"}],"predecessor-version":[{"id":1409,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1406\/revisions\/1409"}],"wp:attachment":[{"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=1406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=1406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wthee.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=1406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}