{"id":383,"date":"2023-02-25T08:39:51","date_gmt":"2023-02-25T00:39:51","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=383"},"modified":"2023-04-30T14:39:09","modified_gmt":"2023-04-30T06:39:09","slug":"retrofit2-learning-5-customize-request-headers","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/retrofit2-learning-5-customize-request-headers\/","title":{"rendered":"Retrofit2\u5b66\u4e60\u4e4b\u4e94\uff1a\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>Retrofit2 \u652f\u6301\u81ea\u5b9a\u4e49\u8bf7\u6c42\u6d88\u606f\u5934\uff0c\u4f7f\u7528 <a target=\"_blank\" rel=\"noopener\" href=\"mailto:code&gt;@Headers&lt;\/code\">code>@Headers<\/code<\/a> \u5b9a\u4e49\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/square.github.io\/retrofit\/\">http:\/\/square.github.io\/retrofit\/<\/a><\/p>\n<p><!-- more --><\/p>\n<h2>\u81ea\u5b9a\u4e49Headers<\/h2>\n<h3>\u521b\u5efa\u4e1a\u52a1\u8bf7\u6c42\u63a5\u53e3<\/h3>\n<pre><code class=\"language-java\">public interface Api {\n\n    \/**\n     * \u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\n     * @return\n     *\/\n    @GET(&quot;user\/info?id=1&quot;)\n    @Headers({&quot;User-Agent: www.AppBlog.cn&quot;, &quot;key: value&quot;})\n    Call&lt;User&gt; getHeaders();\n\n}<\/code><\/pre>\n<h3>\u521b\u5efa\u4e00\u4e2aRetrofit\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u5229\u7528Retrofit\u5b9e\u4f8b\u521b\u5efa\u63a5\u53e3\u5bf9\u8c61\u548c\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-java\">public void getHeaders(View view) {\n    Retrofit retrofit = new Retrofit.Builder()\n            .baseUrl(SERVER_ADDRESS)\n            .addConverterFactory(GsonConverterFactory.create())\n            .build();\n\n    api = retrofit.create(Api.class);\n\n    api.getHeaders().enqueue(new Callback&lt;User&gt;() {\n        @Override\n        public void onResponse(Call&lt;User&gt; call, Response&lt;User&gt; response) {\n            \/\/\u5728UI\u4e3b\u7ebf\u7a0b\u8fd0\u884c\n            if (response.isSuccessful()) {\n                Log.i(TAG, &quot;\u8fd4\u56de\u6210\u529f&quot;);\n            }\n        }\n\n        @Override\n        public void onFailure(Call&lt;User&gt; call, Throwable t) {\n            Log.i(TAG, &quot;\u8bf7\u6c42\u5931\u8d25: &quot; + t.getLocalizedMessage());\n        }\n    });\n}<\/code><\/pre>\n<p>\u8c03\u7528 <code>getHeaders<\/code> \u65b9\u6cd5\uff0c\u670d\u52a1\u7aef\u63a5\u6536\u7684\u7684\u8bf7\u6c42\u5934\u5982\u4e0b\uff0c\u53ef\u89c1\u8bc6\u522b\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u8bf7\u6c42\u5934 User-Agent  \u548c key<\/p>\n<pre><code>&quot;GET \/user\/info?id=1 HTTP\/1.1&quot; 200 -\nKey: value\nContent-Length: \nUser-Agent: www.AppBlog.cn\nConnection: Keep-Alive\nHost: 192.168.1.8:5000\nContent-Type: \nAccept-Encoding: gzip<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb Retrofit2 \u652f\u6301\u81ea\u5b9a\u4e49\u8bf7\u6c42\u6d88\u606f\u5934\uff0c\u4f7f\u7528 code>@Headers<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[130],"tags":[],"class_list":["post-383","post","type-post","status-publish","format-standard","hentry","category-retrofit"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=383"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/383\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}