HttpURLConnection传JSON数据

时间:2024-11-09 08:59:11
try { //创建连接 URL url = new URL(url); HttpURLConnection connection = (HttpURLConnection) (); (true); (true); ("POST"); (false); (true); ("Content-Type", "application/json"); (); // POST请求 DataOutputStream out = new DataOutputStream(()); JSONObject obj = new JSONObject(); String json = ((), "utf-8"); (json); (); (); // 读取响应 BufferedReader reader = new BufferedReader(new InputStreamReader(())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = ()) != null) { lines = (lines, "utf-8"); (lines); } (sb); (); // 断开连接 (); } catch (MalformedURLException e) { (); } catch (UnsupportedEncodingException e) { (); } catch (IOException e) { (); }