I am using org.json.simple.JSONObject. I want to pass a JSONObject using HTTPURLConnection. How can i do this?
Asked
Active
Viewed 634 times
3 Answers
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
-
I cannot use WS for some limitations – Arasu Jan 12 '12 at 11:58
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
-
-
I couldn't exactly remember it, i hope this could help you http://stackoverflow.com/questions/17651395/convert-jsonobject-to-string – Arasu Jun 03 '15 at 10:43