{"id":1386,"date":"2024-06-25T11:33:13","date_gmt":"2024-06-25T03:33:13","guid":{"rendered":"https:\/\/blog.quantoyo.com\/?p=1386"},"modified":"2024-06-25T11:33:13","modified_gmt":"2024-06-25T03:33:13","slug":"java-%e9%80%81%e5%87%ba-application-json","status":"publish","type":"post","link":"https:\/\/blog.quantoyo.com\/?p=1386","title":{"rendered":"JAVA \u9001\u51fa application\/json"},"content":{"rendered":"\n<p>\u5982\u4f55\u4f7f\u7528java \u539f\u751f HttpURLConnection \u9001\u51fa\u4e00\u500b json \u683c\u5f0f\u7684 POST<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u4ee5\u4e0b\u662f\u500b\u7bc4\u4f8b\uff0c\u6703\u5c07\u4e00\u500bjson \u5b57\u4e32\u4f7f\u7528POST\u65b9\u5f0f\u9001\u5f80 slack \u7684\u6a5f\u5668\u4eba\uff0c\u503c\u5f97\u4e00\u63d0\u7684\u662fjson \u5167\u5bb9\u5176\u5be6\u662f\u500b\u5b57\u4e32\uff0c\u5fc5\u9808\u4f7f\u7528writeBytes \u5c07\u8cc7\u6599\u9001\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>\n\n\tpublic static void main(String&#91;] arg) {\n\t\tString QueryStr = \"{\\\"text\\\":\\\"I am a bot make by Quanto !!!   Hello, BOBt!\\\"}\";\n\t\tString ret = \"\";\n\t\tString urlString = \"https:\/\/hooks.slack.com\/services\/T6PQXXXXXXXXX\";\n\t\tString host = \"hooks.slack.com\";\n\t\tString url = \"services\/TXXXXXXXXXXXXXXXXXXXXXXXXX\";\n\t\tret = httpConnectionPostJSON(urlString, QueryStr);\n\t\tSystem.out.println(ret);\n\t}\n\t\n\tpublic static String httpConnectionPostJSON(String apiUrl,String params) {\n        HttpURLConnection conn = null;\n        StringBuilder response = new StringBuilder();\n\n        try {\n            URL url = new URL(apiUrl);\n            conn = (HttpURLConnection) url.openConnection();\n            conn.setRequestProperty(\"Content-Type\",\"application\/json; charset=UTF-8\");\n            conn.setRequestProperty(\"Accept\", \"application\/json\");\n            conn.setRequestMethod(\"POST\");\n            conn.setConnectTimeout(10000);\n            conn.setReadTimeout(10000);\n            conn.setDoInput(true); \/\/\u5141\u8a31\u8f38\u5165\u6d41\uff0c\u5373\u5141\u8a31\u4e0b\u8f09\n            conn.setDoOutput(true); \/\/\u5141\u8a31\u8f38\u51fa\u6d41\uff0c\u5373\u5141\u8a31\u4e0a\u50b3\n            conn.setUseCaches(false); \/\/\u8a2d\u7f6e\u662f\u5426\u4f7f\u7528\u7de9\u5b58\n\n            OutputStream os = conn.getOutputStream();\n            DataOutputStream writer = new DataOutputStream(os);\n            String jsonString = params;\n            writer.writeBytes(jsonString);\n            writer.flush();\n            writer.close();\n            os.close();\n            \/\/Get Response\n            InputStream is = conn.getInputStream();\n\n            BufferedReader reader = new BufferedReader(new InputStreamReader(is));\n            String line;\n\n            while ((line = reader.readLine()) != null) {\n                response.append(line);\n                response.append('\\r');\n            }\n            reader.close();\n        }catch (Exception ex) {\n            ex.printStackTrace();\n        }finally {\n            if(conn!=null) {\n                conn.disconnect();\n            }\n        }\n\n        return response.toString();\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528java \u539f\u751f HttpURLConnection \u9001\u51fa\u4e00\u500b json \u683c\u5f0f\u7684 POST<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[233,237,235,8,51,236,50,234,238],"class_list":["post-1386","post","type-post","status-publish","format-standard","hentry","category-technology","tag-application-json","tag-dataoutputstream","tag-httpurlconnection","tag-java","tag-json","tag-outputstream","tag-post","tag-slack","tag-writebytes"],"_links":{"self":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1386"}],"version-history":[{"count":1,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1386\/revisions"}],"predecessor-version":[{"id":1387,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1386\/revisions\/1387"}],"wp:attachment":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}