{"id":247,"date":"2016-12-30T09:20:47","date_gmt":"2016-12-30T09:20:47","guid":{"rendered":"https:\/\/boochlin.com\/?p=247"},"modified":"2026-09-19T18:04:22","modified_gmt":"2026-09-19T18:04:22","slug":"%e5%a4%a7%e5%ae%b6%e5%a5%bd%ef%bc%8c%e6%a8%99%e9%a1%8c%e5%95%a5%e9%ac%bc%e7%9a%84%e5%ae%8c%e5%85%a8%e9%82%84%e6%b2%92%e6%83%b3%e5%88%b0","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=247","title":{"rendered":"Retrofit 2 \u2014 How to Upload Files and Parameter list to Server"},"content":{"rendered":"<p>Retrofit 2<\/p>\n<p>\u4e00\u500b\u4e45\u805e\u5927\u540d\u7684\u7db2\u8def\u64cd\u4f5c\u6846\u67b6\uff0c\u57fa\u65bc OKHTTP3 \u5728\u5305\u88dd\uff0c\u7528\u8d77\u4f86\u633a\u8212\u723d\u7684\uff0c\u4e4b\u524d\u90fd\u662f\u4f7f\u7528 VOLLEY \u4f5c\u70ba\u4e3b\u8981\u5de5\u5177\uff0c\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u5c01\u88dd\u6982\u5ff5\u4e0d\u540c\u3002\u5c0d\u65bc Retrofit \u4e0a\u50b3\u6a94\u6848\u4e0d\u719f\uff0c\u56e0\u6b64\u50c5\u50c5\u662f\u4f5c\u7b46\u8a18\u3002<\/p>\n<p><a href=\"https:\/\/futurestud.io\/tutorials\/retrofit-2-how-to-upload-files-to-server\">\u4e00\u6b21\u4e0a\u50b3\u4e00\u4efd\u6a94\u6848<\/a><\/p>\n<p><a href=\"https:\/\/futurestud.io\/tutorials\/retrofit-2-how-to-upload-multiple-files-to-server\">\u4e00\u6b21\u4e0a\u50b3\u591a\u4efd\u6a94\u6848<\/a><\/p>\n<p><a href=\"https:\/\/futurestud.io\/tutorials\/retrofit-2-passing-multiple-parts-along-a-file-with-partmap\">\u540c\u6642\u4e0a\u50b3\u6a94\u6848\u8ddf\u53c3\u6578<\/a><\/p>\n<p>\u5176\u5be6\u4e0a\u9762\u5c31\u5df2\u7d93\u8db3\u5920\u5145\u5206\uff0c\u800c\u6211\u7684\u9700\u6c42\u5247\u662f\u540c\u6642\u5728\u52a0\u4e0a string list \u53c3\u6578\u4e0a\u50b3<\/p>\n<p>retrofit2 \u8a3b\u89e3\u5c01\u88dd<\/p>\n<pre><code class=\"hljs language-plaintext\">@Multipart\n        @POST(\"\/poll\")\n        Call createVote(@PartMap Map&lt;String, RequestBody&gt; parametor,\n                                  @Part(\"description\") RequestBody description,\n                                  @Part MultipartBody.Part file\n\n        );<\/code><\/pre>\n<p>Android client code<\/p>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">createVote<\/span><span class=\"hljs-params\">(VoteData voteSetting, List options, File image\n            , VoteDataManager.createVoteResponseCallback callback)<\/span> {\n\n        <span class=\"hljs-type\">Map<\/span>&lt;String, RequestBody&gt; <span class=\"hljs-variable\">parameter<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">HashMap<\/span>&lt;&gt;();\n\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">title<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>), voteSetting.getTitle());\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">maxOption<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>), String.valueOf(voteSetting.getMaxOption()));\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">minOption<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>), String.valueOf(voteSetting.getMinOption()));\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">userCanAddOption<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>)\n                , String.valueOf(voteSetting.getIsUserCanAddOption()));\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">userPanPreviewResult<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>)\n                , String.valueOf(voteSetting.getIsCanPreviewResult()));\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">security<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>)\n                , VoteData.SECURITY_PUBLIC);\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">category<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>), String.valueOf(voteSetting.getCategory()));\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">startTime<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>), String.valueOf(voteSetting.getStartTime()));\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">endTime<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>), String.valueOf(voteSetting.getEndTime()));\n\n        RequestBody rbOption;\n        <span class=\"hljs-comment\">\/\/ STRING LIST PARAMETOR<\/span>\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> <span class=\"hljs-variable\">i<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">0<\/span>; i &lt; options.size(); i++) {\n            rbOption = RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>), options.get(i));\n            parameter.put(<span class=\"hljs-string\">&quot;pt[&quot;<\/span> + i + <span class=\"hljs-string\">&quot;]&quot;<\/span>, rbOption);\n        }\n\n        parameter.put(<span class=\"hljs-string\">&quot;t&quot;<\/span>, title);\n        parameter.put(<span class=\"hljs-string\">&quot;max&quot;<\/span>, maxOption);\n        parameter.put(<span class=\"hljs-string\">&quot;min&quot;<\/span>, minOption);\n        parameter.put(<span class=\"hljs-string\">&quot;add&quot;<\/span>, userCanAddOption);\n        parameter.put(<span class=\"hljs-string\">&quot;res&quot;<\/span>, userPanPreviewResult);\n        parameter.put(<span class=\"hljs-string\">&quot;sec&quot;<\/span>, security);\n        parameter.put(<span class=\"hljs-string\">&quot;cat&quot;<\/span>, category);\n        parameter.put(<span class=\"hljs-string\">&quot;on&quot;<\/span>, startTime);\n        parameter.put(<span class=\"hljs-string\">&quot;off&quot;<\/span>, endTime);\n\n        <span class=\"hljs-keyword\">if<\/span> (voteSetting.getIsNeedPassword()) {\n            <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">password<\/span> <span class=\"hljs-operator\">=<\/span> RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;text\/plain&quot;<\/span>), voteSetting.password);\n            parameter.put(<span class=\"hljs-string\">&quot;p&quot;<\/span>, password);\n        }\n\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">requestFile<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">null<\/span>;\n        MultipartBody.<span class=\"hljs-type\">Part<\/span> <span class=\"hljs-variable\">body<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">null<\/span>;\n        <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">descriptionString<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">&quot;vote_image&quot;<\/span>;\n        <span class=\"hljs-type\">RequestBody<\/span> <span class=\"hljs-variable\">description<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">null<\/span>;\n\n        <span class=\"hljs-keyword\">if<\/span> (image != <span class=\"hljs-literal\">null<\/span>) {\n            requestFile = RequestBody.create(MediaType.parse(<span class=\"hljs-string\">&quot;multipart\/form-data&quot;<\/span>), image);\n            body = MultipartBody.Part.createFormData(<span class=\"hljs-string\">&quot;i&quot;<\/span>, image.getName(), requestFile);\n            description = RequestBody.create(\n                    MediaType.parse(<span class=\"hljs-string\">&quot;multipart\/form-data&quot;<\/span>), descriptionString);\n        }\n\n        <span class=\"hljs-type\">Call<\/span> <span class=\"hljs-variable\">call<\/span> <span class=\"hljs-operator\">=<\/span> voteService.createVote(parameter, description, body);\n        call.enqueue(callback);\n    }<\/code><\/pre>\n<p>\u96d6\u7136\u4e0a\u9762\u9019\u6a23\u5beb\u597d\u50cf\u5f88\u8822\uff0c\u4f46\u4e00\u6642\u9593\u4e5f\u627e\u4e0d\u5230\u600e\u67d0\u5beb\uff0c<\/p>\n<p>\u5982\u679c\u6709\u8208\u8da3\u7684\u4eba\uff0c\u61c9\u8a72\u8981\u66f4\u52a0\u9032\u4e00\u6b65\u7406\u89e3 multipart\/form-data<\/p>\n<p>\u6700\u8fd1\u5be6\u5728\u662f\u592a\u5fd9\uff0c\u6587\u7ae0\u8cea\u91cf\u63d0\u6607\u4e0d\u8d77\u4f86<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Retrofit 2 \u4e00\u500b\u4e45\u805e\u5927\u540d\u7684\u7db2\u8def\u64cd\u4f5c\u6846\u67b6\uff0c\u57fa\u65bc OKHTTP3 \u5728\u5305\u88dd\uff0c\u7528\u8d77\u4f86\u633a\u8212\u723d\u7684\uff0c\u4e4b\u524d\u90fd\u662f\u4f7f\u7528  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[237,264,65],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/247"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=247"}],"version-history":[{"count":4,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":1581,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/247\/revisions\/1581"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}