Dafi
Dafi

Reputation: 1

getResponseCode returns null

the getResponseCode method returns null in the below code, there's no other thing happening in my class. I am just trying to receive the JSON response through my request. What might be the reason?

String accessToken = "";
        String token = "";

        // Create Basic Auth
        String auth = "riot"+":"+lockFilePass;
        byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
        String authHeaderValue = "Basic "+new String(encodedAuth);

        String responseToSend = "";

        try {
            // Replace with your API URL
            String apiUrl = "https://127.0.0.1:"+port+"/entitlements/v1/token";


            // Create a JSON object

            // Convert JSON object to string

            // Create URL object
            URL url = new URL(apiUrl);

            // Open a connection
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // Set request method to POST
            connection.setRequestMethod("GET");

            // Enable input and output streams
            connection.setDoOutput(true);

            connection.setAllowUserInteraction(false);

            // Set request headers (Set Basic Auth)
            connection.setRequestProperty("Authorization", authHeaderValue);
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");


            // Write JSON data to the connection

            System.out.println("yes1");

            System.out.println(connection.getURL());

            // Get the response code
            int responseCode = connection.getResponseCode();



            String responseMessage = connection.getResponseMessage();
            //System.out.println("Response Code: " + responseCode);
            System.out.println("Response Message: "+responseMessage);

            accessToken = connection.getHeaderField("accessToken");
            token = connection.getHeaderField("token");
            System.out.println("ACCESS: "+accessToken);
            System.out.println("JUSTTOKEN: "+token);

            // Read the response from the server
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            try {
                String line;
                StringBuilder response = new StringBuilder();

                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                // Print the response
                System.out.println("Response: " + response.toString());
                if (responseCode!=500)
                    responseToSend = response.toString();
            }catch (Exception e){
                e.printStackTrace();
            }

            reader.close();
            // Close the connection
            connection.disconnect();

            System.out.println("1: "+responseMessage+" 2: "+responseToSend);

            List<String> responseList = new ArrayList<>();
            responseList.add(responseToSend);
            responseList.add(accessToken);
            responseList.add(token);

            //return responseToSend;
            return responseList;
        } catch (Exception e) {
            //e.printStackTrace();
            //return "Couldn't return response from /request.";
            return null;
        }

I've been using the same structure to send requests for both my own application and others. Just didn't work with this one and not sure why.

Edit There goes the error:

java.lang.NullPointerException
    at com.daffy.BotHandler.onMessageReceived(BotHandler.java:108)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:446)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:88)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:70)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:177)
    at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:138)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:39)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:1015)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:901)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:879)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:1054)
    at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
    at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
    at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
    at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
    at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)

Upvotes: 0

Views: 77

Answers (0)

Related Questions