{"id":382,"date":"2023-02-25T08:39:12","date_gmt":"2023-02-25T00:39:12","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=382"},"modified":"2023-04-30T14:39:17","modified_gmt":"2023-04-30T06:39:17","slug":"retrofit2-learning-4-post-request","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/retrofit2-learning-4-post-request\/","title":{"rendered":"Retrofit2\u5b66\u4e60\u4e4b\u56db\uff1aPOST\u8bf7\u6c42"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>Retrofit2 POST\u8bf7\u6c42\u7684\u8bf7\u6c42\u53c2\u6570\u652f\u6301Body\u3001FormUrlEncoded\u3001Multipart\u5f62\u5f0f\u3002<\/p>\n<ul>\n<li>Body\uff1aConverter\u8f6c\u6362\u5668\u652f\u6301\u7684\u683c\u5f0f\uff08\u5982JSON\uff09<\/li>\n<li>FormUrlEncoded\uff1aForm\u8868\u5355\u5f62\u5f0f<\/li>\n<li>Multipart\uff1a\u652f\u6301\u6587\u4ef6\u63d0\u4ea4<\/li>\n<\/ul>\n<p><!-- more --><\/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<h2>POST\u8bf7\u6c42<\/h2>\n<h3>\u521b\u5efa\u4e1a\u52a1\u8bf7\u6c42\u63a5\u53e3<\/h3>\n<pre><code class=\"language-java\">public interface Api {\n\n    \/**\n     * Body\u53c2\u6570\n     * @param user\n     * @return\n     *\/\n    @POST(&quot;user\/new&quot;)\n    Call&lt;ResponseResult&gt; saveUser(@Body User user);\n\n    \/**\n     * \u8868\u5355\u53c2\u6570\n     * @param userId\n     * @param userName\n     * @return\n     *\/\n    @FormUrlEncoded\n    @POST(&quot;user\/edit&quot;)\n    Call&lt;ResponseResult&gt; editUser(@Field(&quot;id&quot;) int userId, @Field(&quot;username&quot;) String userName);\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 postRequest(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    User user = new User();\n    user.setId(8);\n    user.setUsername(&quot;AppBlog.CN&quot;);\n\n    \/\/api.saveUser(user).enqueue(new Callback&lt;ResponseResult&gt;() {\n    api.editUser(8, &quot;AppBlog.CN&quot;).enqueue(new Callback&lt;ResponseResult&gt;() {\n        @Override\n        public void onResponse(Call&lt;ResponseResult&gt; call, Response&lt;ResponseResult&gt; response) {\n            \/\/\u5728UI\u4e3b\u7ebf\u7a0b\u8fd0\u884c\n            if (response.isSuccessful()) {\n                Log.i(TAG, &quot;\u8fd4\u56de\u6210\u529f&quot;);\n                ResponseResult result = response.body();\n                if (result != null) {\n                    Toast.makeText(MainActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show();\n                }\n            }\n        }\n\n        @Override\n        public void onFailure(Call&lt;ResponseResult&gt; call, Throwable t) {\n            Log.i(TAG, &quot;\u8bf7\u6c42\u5931\u8d25: &quot; + t.getLocalizedMessage());\n        }\n    });\n}<\/code><\/pre>\n<p>\u4f9d\u6b21\u8c03\u7528 <code>saveUser<\/code> \u548c <code>editUser<\/code> \uff0c\u4ee5 Body \u548c Field \u53c2\u6570\u7684\u5f62\u5f0f\u53d1\u9001 POST \u8bf7\u6c42\uff0c\u670d\u52a1\u7aef\u63a5\u6536\u7684\u683c\u5f0f\u5206\u522b\u4e3aJSON\uff08\u901a\u8fc7GsonConverterFactory\u8f6c\u6362Body\uff09\u548c\u8868\u5355\u53c2\u6570\u3002<\/p>\n<pre><code>&quot;POST \/user\/new HTTP\/1.1&quot; 200 -\n{u&#039;username&#039;: u&#039;AppBlog.CN&#039;, u&#039;id&#039;: 8}\n\n&quot;POST \/user\/edit HTTP\/1.1&quot; 200 -\nid = 8\nusername = AppBlog.CN<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb Retrofit2 POST\u8bf7\u6c42\u7684\u8bf7\u6c42\u53c2\u6570\u652f\u6301Body\u3001FormUrlEncoded\u3001Multipart [&hellip;]<\/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-382","post","type-post","status-publish","format-standard","hentry","category-retrofit"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/382","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=382"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}