Java POST Issue, how to check return from server?

  Kiến thức lập trình

How to check if is true or false ? Using executor but success seems not to change its value.. there is any trick to make it in a ‘easy’ way ? Im able to reach inside the try catch, but seems that its not working properly, im not changing its value truly.. maybe if i make success a atribute of the class ? Chatgpt is in crazy mode and seems not to solve the problem for me..

    public Boolean  POST(String payload, String action) {
            
            AtomicReference<Boolean> success = new AtomicReference<Boolean>(false);

        executor.execute(() -> {
            try {


                Gson gJSON = new Gson();
                // Convert payload to a JSON object
                JSONObject jsonObject = new JSONObject(payload);

                // Add the action property to the JSON object
                jsonObject.put("action", action);

                // Convert the modified JSON object back to a string
                String payloadWithAction = gJSON.toJson(jsonObject);
                //System.out.println("Payload to SV"+payloadWithAction);
                // Create URL object
                URL url = new URL("https://moveroute.org/php/run-register-api.php");

                // Open connection
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setDoOutput(true);
                Response responseObject = new Response();


                // Write JSON data to connection
                try (OutputStream os = connection.getOutputStream()) {
                    os.write(payloadWithAction.getBytes());
                }

                // Check response code
                int responseCode = connection.getResponseCode();
                StringBuilder response = new StringBuilder();
                Response responseJSON = null;
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    // Handle success
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                    String jsonResponse = response.toString();
                    //System.out.println("Response from server: " + jsonResponse);

                    responseJSON = gJSON.fromJson(jsonResponse, Response.class);
                    success.set(responseJSON.getStatus().booleanValue());
                    System.out.println(responseJSON.getMessage());
                } else {
                    //ERROR FAIL

                    responseObject.setStatus(false);
                    responseObject.setMessage("Impossible to make the requested action.");

                }

                // Close connection
                connection.disconnect();

            } catch (Exception e) {
                e.printStackTrace();
            }

        });
        return success.get();
    }

Just making the login of an app

LEAVE A COMMENT