POST JSON in Java

Code to POST JSON in Java

Since it is not necessary to save the data that I want to have temporarily in the DB, I made it possible to post and share it as a JSON String.

postJSON


	public String postJson(String json, String path) {
		HttpURLConnection uc;
		try {
			URL url = new URL("http://host"+path);
			uc = (HttpURLConnection) url.openConnection();
			uc.setRequestMethod("POST");
			uc.setUseCaches(false);
			uc.setDoOutput(true);
			uc.setRequestProperty("Content-Type", "application/json; charset=utf-8");
			OutputStreamWriter out = new OutputStreamWriter(
				new BufferedOutputStream(uc.getOutputStream()));
			out.write(json);
			out.close();

			BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
			String line = in.readLine();
			String body = "";
			while (line != null) {
				body = body + line;
				line = in.readLine();
			}
			uc.disconnect();
			return body;
		} catch (IOException e) {
			e.printStackTrace();
			return "client - IOException : " + e.getMessage();
		}
	}

Servlet side code

Receives POSTed data and stores it as JSONArray and JSONObject.

doPost


	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		BufferedReader br = new BufferedReader(request.getReader());
		String line = br.readLine();
		String body = "";
		if (line != null) {
			body = body + line;
			line = br.readLine();
		}
		PrintWriter out = response.getWriter();
		out.println("{\"status\":\"OK\"}");
		out.flush();
		out.close();

		try {
			LinkedList todo = new LinkedList();
			JSONArray ja = new JSONArray(body);
			for (int i = 0; i < ja.length(); i++) {
				JSONObject tdj = (JSONObject) ja.get(i);
				//Processing addition
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

Recommended Posts

POST JSON in Java
POST Json in Java ~ HttpURLConnection ~
Read JSON in Java
Create JSON in Java
Json serialization / deserialization in Java 1.4
[java] HttpURLConnection POST
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Get the result of POST in Java
Try using JSON format API in Java
Working with huge JSON in Java Lambda
[java] sort in list
Interpreter implementation in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Code to escape a JSON string in Java
Parse Json in Scala
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Express failure in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
Read binary files in Java 1
Avoid Yubaba's error in Java
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Write flyway callbacks in Java
Topic Analysis (LDA) in Java