{"id":294,"date":"2022-09-30T21:14:37","date_gmt":"2022-09-30T13:14:37","guid":{"rendered":"http:\/\/blog.nhl520.cn\/?p=294"},"modified":"2022-09-30T21:18:10","modified_gmt":"2022-09-30T13:18:10","slug":"android-okhttp%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/blog.nhl520.cn\/index.php\/2022\/09\/30\/android-okhttp%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6\/","title":{"rendered":"Android okhttp\u4e0a\u4f20\u6587\u4ef6"},"content":{"rendered":"\n<p>\u8fd9\u4e2a\u9700\u8981\u914d\u5408PHP\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> \/\/\u9700\u8981\u5728build.gradle\u6dfb\u52a0\n implementation 'com.squareup.okhttp3:okhttp:3.7.0'\n implementation 'com.squareup.okio:okio:1.13.0'<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> File file = new File(picturePath);\n \/\/\u4e0a\u4f20\u6587\u4ef6\u7684\u683c\u5f0f\n String img_format = \"image\/*\";\n \/\/\u5c01\u88c5\u6587\u4ef6\u683c\u5f0f\n RequestBody requestFormat = RequestBody.create(MediaType.parse(img_format),file);\n \/\/\u6587\u4ef6\u5185\u5bb9\n RequestBody requestBody = new MultipartBody.Builder()\n             .setType(MultipartBody.FORM)\n             .addFormDataPart(\"file\",file.getName(),requestFormat)\n             .build();\n             \/\/Okhttp\u7f51\u7edc\u6570\u636e\u4f20\u9012\nOkHttpClient client = new OkHttpClient.Builder()\n             .build();\nRequest request = new Request.Builder()\n            .url(\u94fe\u63a5)\n            .post(requestBody)\n            .build();\n             Call call = client.newCall(request);\n             call.enqueue(new Callback() {\n             @Override\n             public void onFailure(Call call, IOException e) {\n             Log.i(\"onFailure\",e.getMessage());\n             }\n             @Override\n             public void onResponse(Call call, Response response) throws IOException {\n             Log.i(\"\u4e0a\u4f20\u81f3\u670d\u52a1\u5668\u6210\u529f\",response.body().string());\n          }\n});<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u9700\u8981\u914d\u5408PHP\u4f7f\u7528<\/p>\n","protected":false},"author":1,"featured_media":172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":5,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":299,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions\/299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/media\/172"}],"wp:attachment":[{"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}