如何在HTTPURLConnection中传递JSON对象

如何在HTTPURLConnection中传递JSON对象,json,playframework,Json,Playframework,我正在使用org.json.simple.JSONObject。我想使用HTTPURLConnection传递一个JSONObject。我如何才能做到这一点?为什么不使用play.libs.WS类,并将此JSON对象设置为指定参数名的字符串。我将JSONObject转换为字符串,并将字符串转换为目标中的JSONObject。至少现在是这样。回答这个问题可能已经晚了,但为了以防万一,我会将用于该目标的代码粘贴到下面: URL url; HttpURLConnection urlConnection

我正在使用org.json.simple.JSONObject。我想使用HTTPURLConnection传递一个JSONObject。我如何才能做到这一点?

为什么不使用
play.libs.WS
类,并将此JSON对象设置为指定参数名的字符串。

我将JSONObject转换为字符串,并将字符串转换为目标中的JSONObject。至少现在是这样。

回答这个问题可能已经晚了,但为了以防万一,我会将用于该目标的代码粘贴到下面:

URL url;
HttpURLConnection urlConnection = null;
 try {
  url = new URL(SOME URL...);
  JSONObject reqJson = THE JSON OBJECT TO BE PASSED...;
  urlConnection = (HttpURLConnection) url.openConnection();
  urlConnection.setDoOutput(true);
  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
  bw.write(reqJson.toString());
  bw.flush();
  bw.close();

  //Now handle response
  BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
   ...
   ...

  } catch (Exception e) {
     e.printStackTrace();
  } finally {
    urlConnection.disconnect();
}

我不能使用WS,因为有一些限制。我可能知道您是如何使用它的。我无法准确地记住它,我希望这可以帮助您