{"id":368,"date":"2023-02-25T08:27:45","date_gmt":"2023-02-25T00:27:45","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=368"},"modified":"2023-04-30T14:41:18","modified_gmt":"2023-04-30T06:41:18","slug":"okhttp3-learning-4-post-request-form","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/okhttp3-learning-4-post-request-form\/","title":{"rendered":"OKHttp3\u5b66\u4e60\u4e4b\u56db\uff1aPOST\u8bf7\u6c42\uff08Form\u8868\u5355\u5f62\u5f0f\uff09"},"content":{"rendered":"<p>\u9700\u6c42\uff1a\u5b9e\u73b0\u7528\u6237\u767b\u9646\uff0c\u767b\u5f55\u4fe1\u606f\u4ee5Form\u8868\u5355\u6570\u636e\u7684\u5f62\u5f0f\u53d1\u9001<\/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 postRequest(View view) {\n    String username = mEtUserName.getText().toString().trim();\n    String password = mEtPassWord.getText().toString().trim();\n    loginWithForm(username, password);\n}\n\nprivate void loginWithForm(String username, String password) {\n    OkHttpClient client = new OkHttpClient();\n    String url = SERVER_ADDRESS + &quot;\/login&quot;;\n    \/\/\u8bbe\u7f6eForm\u8868\u5355\u6570\u636e\n    RequestBody body = new FormBody.Builder()\n            .add(&quot;username&quot;, username)\n            .add(&quot;password&quot;, password)\n            .build();\n    Request request = new Request.Builder().url(url).post(body).build();\n\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                ResponseBody body = response.body();\n                if (body != null) {\n                    String result = body.string();\n                    Log.i(TAG, &quot;\u8fd4\u56de\u6210\u529f: &quot; + result);\n                    body.close();\n\n                    JSONObject json = null;\n                    try {\n                        json = new JSONObject(result);\n                        final int code = json.optInt(&quot;code&quot;);\n                        final String message = json.optString(&quot;message&quot;);\n                        runOnUiThread(new Runnable() {\n                            @Override\n                            public void run() {\n                                if (code == 1) {\n                                    Log.i(TAG, &quot;\u767b\u5f55\u6210\u529f&quot;);\n                                } else {\n                                    Log.i(TAG, &quot;\u767b\u5f55\u5931\u8d25&quot;);\n                                }\n                                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();\n                            }\n                        });\n                    } catch (JSONException e) {\n                        e.printStackTrace();\n                    }\n                }\n            }\n        }\n    });\n}<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u8fd4\u56de\u6570\u636e<\/p>\n<pre><code>{\n    &quot;code&quot;: 1,\n    &quot;message&quot;: &quot;\u767b\u9646\u6210\u529f&quot;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a\u5b9e\u73b0\u7528\u6237\u767b\u9646\uff0c\u767b\u5f55\u4fe1\u606f\u4ee5Form\u8868\u5355\u6570\u636e\u7684\u5f62\u5f0f\u53d1\u9001 \u5728app\u6a21\u5757\u7684build.gradle\u914d\u7f6e comp [&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-368","post","type-post","status-publish","format-standard","hentry","category-okhttp"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/368","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=368"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/368\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}