0

I am using org.json.simple.JSONObject. I want to pass a JSONObject using HTTPURLConnection. How can i do this?

Arasu
  • 2,078
  • 6
  • 40
  • 67

3 Answers3

1

It's probably late for this answer but, just in case, I paste below the code I use for that goal:

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();
}
leo9r
  • 2,037
  • 26
  • 30
0

Why not use the play.libs.WS class, and set this JSON object as a string to a specified parameter name.

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
0

I converted JSONObject as a string and converted the string as JSONObject in the target. Atleast for now.

Arasu
  • 2,078
  • 6
  • 40
  • 67