{"id":369,"date":"2023-02-25T08:28:25","date_gmt":"2023-02-25T00:28:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=369"},"modified":"2023-04-30T14:41:24","modified_gmt":"2023-04-30T06:41:24","slug":"okhttp3-learning-5-post-request-json","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/okhttp3-learning-5-post-request-json\/","title":{"rendered":"OKHttp3\u5b66\u4e60\u4e4b\u4e94\uff1aPOST\u8bf7\u6c42\uff08JSON\u53c2\u6570\u5f62\u5f0f\uff09"},"content":{"rendered":"<p>\u9700\u6c42\uff1a\u5b9e\u73b0\u7528\u6237\u767b\u9646\uff0c\u767b\u5f55\u4fe1\u606f\u4ee5JSON\u53c2\u6570\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    loginWithJson(username, password);\n}\n\nprivate void loginWithJson(String username, String password) {\n    OkHttpClient client = new OkHttpClient();\n    String url = SERVER_ADDRESS + &quot;\/login\/json&quot;;\n\n    \/\/\u6784\u5efaJSON\u53c2\u6570\n    JSONObject json = new JSONObject();\n    try {\n        json.put(&quot;username&quot;, username);\n        json.put(&quot;password&quot;, password);\n    } catch (JSONException e) {\n        e.printStackTrace();\n    }\n    String jsonParams = json.toString();\n    Log.i(TAG, &quot;json params: &quot; + jsonParams);\n\n    \/\/\u8bbe\u7f6e Content-Type\u4e3a application\/json\n    RequestBody body = RequestBody.create(MediaType.parse(&quot;application\/json&quot;), jsonParams);\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>\u8bf7\u6c42JSON\u53c2\u6570<\/p>\n<pre><code>json params: {&quot;username&quot;:&quot;Joe.Ye&quot;,&quot;password&quot;:&quot;123456&quot;}<\/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\u4ee5JSON\u53c2\u6570\u7684\u5f62\u5f0f\u53d1\u9001 \u5728app\u6a21\u5757\u7684build.gradle\u914d\u7f6e compil [&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-369","post","type-post","status-publish","format-standard","hentry","category-okhttp"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/369","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=369"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/369\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}