0

I am trying do create a java client application to do some work on. All of the work that can be done on said java client should be uploaded to a Webserver, so it can be looked at by different people. To get that to work I needed to request whole web pages, which I got to work fine, and I needed to make some ajax like calls to the server. While I found a solution on the internet that should work, and partially does. It connects to the server and actually receives an answer, but I can't seem to send any information to the server, like an username or some other information.

My serverside php code:

<?php

session_start();

if (!function_exists("login")) {
    function login($username, $password) {
        include "../sessionManager.php";
        if (checkLoginCred($username, $password)) {
            loginUser(false, $username);
            header('Content-Type: application/json');
            echo json_encode(array('process' => 'success', 'userID' => $_SESSION['user']->ID));
            exit();
        }
        $reason = usernameExists($username) ? 'password' : 'username';
        $array = array('process' => 'failure', 'reason' => $reason);
        header('Content-Type: application/json');
        echo json_encode($array);
        exit();
    }
}

if (isset($_POST['username']) && isset($_POST['password'])) {
    login($_POST['username'], $_POST['password']);
} else {
    $reason = 'unknown';
    $array = array('process' => 'failure', 'reason' => $reason);
    header('Content-Type: application/json');
    echo json_encode($array);
    exit();
}

My clientside java code(JsonObject is from the gson library, AjaxSuccess and AjaxError are Single Method Interfaces):

public static void ajax(final String urlString, JsonObject data, AjaxSuccess success, AjaxError error) {
        new Thread(() -> {
            try {
                final URL url = new URL(urlString);
                final URLConnection connection = url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                connection.connect();
                final OutputStream outputStream = connection.getOutputStream();
                String strData = data.toString();
                strData = strData.substring(1, strData.length()-2);
                outputStream.write(strData.getBytes(StandardCharsets.UTF_8));
                outputStream.flush();
                final InputStream inputStream = connection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder input = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    input.append(line);
                }
                JsonObject object = new JsonParser().parse(input.toString()).getAsJsonObject();
                success.onSuccess(object);
            } catch (IOException e) {
                error.onError(e);
            }
        }).start();
    }

The server currently answers with: { 'process': 'failure', 'reason': 'unknown' }

I also checked the POST variables content, which was undefined, like it was to be expected.

  • Are you forced to work with URLConnection? Using libraries like the Apache HttpComponents (HttpClient) makes life so much easier. – Simon Aug 01 '20 at 13:43
  • I am not forced to work with URLConnection, but I usually try to code as much as possible on my own and I was sure I could do an ajax call without a library – NoobyPilot Aug 01 '20 at 13:59
  • lê-hoàng-dững I think that is pretty much what I was trying to find, thanks for your help – NoobyPilot Aug 01 '20 at 14:04

0 Answers0