{"id":372,"date":"2023-02-25T08:30:47","date_gmt":"2023-02-25T00:30:47","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=372"},"modified":"2023-04-30T14:40:22","modified_gmt":"2023-04-30T06:40:22","slug":"okhttp3-learning-8-upload-files-with-parameters","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/okhttp3-learning-8-upload-files-with-parameters\/","title":{"rendered":"OKHttp3\u5b66\u4e60\u4e4b\u516b\uff1a\u5e26\u53c2\u6570\u6587\u4ef6\u4e0a\u4f20"},"content":{"rendered":"<p>\u9700\u6c42\uff1a\u4e0a\u4f20\u624b\u673aSD\u5361\u4e0a\u7684\u6587\u4ef6\u81f3\u7f51\u7edc\u670d\u52a1\u5668\u7aef\uff0c\u8981\u6c42\u540c\u65f6\u9644\u5e26Form\u8868\u5355\u53c2\u6570\u548c\u6587\u4ef6\u53c2\u6570<\/p>\n<p>\u5728app\u6a21\u5757\u7684build.gradle\u914d\u7f6e<\/p>\n<p><!-- more --><\/p>\n<pre><code>compile &#039;com.squareup.okhttp3:okhttp:3.4.2&#039;<\/code><\/pre>\n<p>\u5b9e\u73b0\u6e90\u7801<\/p>\n<pre><code class=\"language-java\">public void uploadFile(View view) {\n    OkHttpClient client = new OkHttpClient();\n    String uploadUrl = SERVER_ADDRESS + &quot;\/upload&quot;;\n    String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + &quot;generated.apk&quot;;\n    File file = new File(filePath);\n\n    RequestBody fileBody = RequestBody.create(MediaType.parse(&quot;application\/octet-stream&quot;), file);\n\n    RequestBody requestBody = new MultipartBody.Builder()\n            .setType(MultipartBody.FORM)\n            .addFormDataPart(&quot;username&quot;, &quot;appblog.cn&quot;)  \/\/Form\u8868\u5355\u53c2\u6570\n            .addFormDataPart(&quot;file&quot;, &quot;appblog.apk&quot;, fileBody)  \/\/\u6587\u4ef6\u53c2\u6570\n            .build();\n\n    Request request = new Request.Builder().url(uploadUrl).post(requestBody).build();\n    client.newCall(request).enqueue(new Callback() {\n        @Override\n        public void onFailure(Call call, IOException e) {\n            Log.i(TAG, &quot;\u8bf7\u6c42\u5931\u8d25: &quot; + e.getLocalizedMessage());\n        }\n\n        @Override\n        public void onResponse(Call call, Response response) throws IOException {\n            if (response.isSuccessful()) {\n                Log.i(TAG, &quot;\u8bf7\u6c42\u6210\u529f&quot;);\n            }\n        }\n    });\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a\u4e0a\u4f20\u624b\u673aSD\u5361\u4e0a\u7684\u6587\u4ef6\u81f3\u7f51\u7edc\u670d\u52a1\u5668\u7aef\uff0c\u8981\u6c42\u540c\u65f6\u9644\u5e26Form\u8868\u5355\u53c2\u6570\u548c\u6587\u4ef6\u53c2\u6570 \u5728app\u6a21\u5757\u7684build. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[116],"tags":[],"class_list":["post-372","post","type-post","status-publish","format-standard","hentry","category-okhttp"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/372","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=372"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/372\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}