{"id":186,"date":"2020-03-17T12:58:09","date_gmt":"2020-03-17T04:58:09","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=186"},"modified":"2020-05-19T14:32:12","modified_gmt":"2020-05-19T06:32:12","slug":"retrofit-%e5%a2%9e%e5%8a%a0cookie","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/retrofit-%e5%a2%9e%e5%8a%a0cookie\/","title":{"rendered":"Retrofit \u589e\u52a0Cookie"},"content":{"rendered":"<h4>1.\u5efa\u7acb\u516c\u5171\u7684Application\u985e<\/h4>\n<pre><code class=\"language-Java line-numbers\">public class MyApplication extends Application {\n    @SuppressLint(\"StaticFieldLeak\")\n    private static Context context;\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        context = getApplicationContext();\n    }\n\n    public static Context getContext() {\n        return context;\n    }\n}\n<\/code><\/pre>\n<h4>2.\u5728\u7b2c\u4e00\u6b21\u767b\u5165\u628aResponse\u56de\u50b3\u7684Cookie\u5b58\u8d77\u4f86<\/h4>\n<p>\u767b\u5165\u56de\u50b3\u7684onResponse<\/p>\n<pre><code class=\"language-Java line-numbers\"> HashSet&lt;String&gt; cookies = new HashSet&lt;&gt;();\n cookies.add(response.headers().get(\"Set-Cookie\"));\n SharedPreferences.Editor config = act.getSharedPreferences(\"config\", MODE_PRIVATE).edit();\n config.putStringSet(\"cookie\", cookies);\n config.apply();\n<\/code><\/pre>\n<h4>3.\u672a\u4f86\u8abf\u7528 \u65b0\u589e\u4e00\u500b\u985e\u5225 \u5be6\u73feInterceptor<\/h4>\n<pre><code class=\"language-Java line-numbers\">public class AddCookiesInterceptor implements Interceptor {\n\n    @NonNull\n    @Override\n    public Response intercept(Chain chain) throws IOException {\n        Request.Builder builder = chain.request().newBuilder();\n        HashSet preferences = (HashSet) MyApplication.getContext().getSharedPreferences(\"config\",\n                Context.MODE_PRIVATE).getStringSet(\"cookie\", null);\n        if (preferences != null) {\n            for (Object cookie : preferences) {\n                builder.addHeader(\"Cookie\", String.valueOf(cookie));\n            }\n        }\n        return chain.proceed(builder.build());\n    }\n}\n<\/code><\/pre>\n<h4>4.RetrofitUtils<\/h4>\n<pre><code class=\"language-Java line-numbers\">public class RetrofitUtils {\n    public static Retrofit getInstance() {\n\n        \/\/\u4fdd\u5b58cookie\n        OkHttpClient client = new OkHttpClient.Builder()\n                .addInterceptor(new AddCookiesInterceptor())\n                .build();\n\n        Retrofit build = new Retrofit.Builder()\n                .baseUrl(\"url\") \/\/Server URL\n                .client(client)\n                .addConverterFactory(GsonConverterFactory.create())\n                .build();\n        return build;\n    }\n}\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u5efa\u7acb\u516c\u5171\u7684Application\u985e public class MyApplication extends  &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[10],"tags":[13,14,38],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-retrofit","tag-android","tag-java","tag-retrofit"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":494,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/186\/revisions\/494"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}