Jump to content

Java 8: How do I do an API request in Java 8?

HydrogenOS

 

Hello,

I am trying to do a API request in Java 8 and the code I am using is for newer versions:

//Classes and Packages Import
package com.malcolmjh.runsafeprofiles.commands;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
//Chat Command Code
public class isprofilesuptodatecommand implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
        //Check if the command sender is a player.
        if (sender instanceof Player) {
            Player p = (Player) sender;
            //Check if the command has arguments.
            if (args.length == 0) {
                //If no args return the error
                p.sendMessage(ChatColor.RED + "Unknown or incomplete command, see below for error");
                p.sendMessage(ChatColor.GRAY + "chat" + ChatColor.RED + "<--[HERE]");
                p.sendMessage(ChatColor.DARK_GREEN + "Usage: /chat <message>");
            }else {
                //Make the "name" var Pt1
                StringBuilder builder = new StringBuilder();
                for(int i = 0; i < args.length; i++) {
                    builder.append(args[i]);
                    builder.append(" ");
                }
                //Make the "name" var Pt2
                String name = builder.toString();
                name = name.stripTrailing();
                //Chat API call
                HttpRequest request = HttpRequest.newBuilder()
                        .uri(URI.create("http://api.malcolmjh.com:8080/check_name?name=" + name))
                        .method("GET", HttpRequest.BodyPublishers.noBody())
                        .build();
                HttpResponse<String> response = null;
                try {
                    response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // Print the response
                p.sendMessage(ChatColor.DARK_BLUE + response.body());
            }
        }
        return true;
    }
}

does anyone know how to do a API request in java 8?

Link to comment
Share on other sites

Link to post
Share on other sites

Any reason you're still stuck on Java 8? In any case, there are many ways to make API calls. Personally, I would try to avoid doing all the low level stuff by hand these days.

 

For example you could use OkHTTP, which works on Java 8 or newer. And if the API is HTTP based, then Retrofit is a good library to use on top of OkHTTP. It allows you to write a simple interface, containing a few annotated method. You can call these methods like other regular Java methods and under the hood they will make the appropriate HTTP API calls for you.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×