OneConfig V1
  • Welcome
  • Documentation license
  • Introduction
    • Getting started
  • Configuration
    • Creating your config
      • Option dependencies & hiding options
      • Listening for option changes
    • Available options
      • Boolean options
        • Switch option
        • Checkbox option
      • Number options
        • Slider option
        • Number option
      • Selector options
        • Dropdown option
        • Radio button option
      • Text option
      • Color option
      • Keybind option
      • Buttons
      • Accordions
      • Decorations
    • Creating your own options
  • Commands
    • Annotation-based commands
    • Tree-based commands
  • Events
    • Using the events system
      • Callback events
      • Subscriber events
  • Utilities Module
    • Hypixel utilities
    • IO utilities
    • Networking utilities
    • JSON utilities
    • Multithreading
    • Platform-specific utilities
  • Migration
    • Temporary V0 -> V1 migration guide
    • Migrating your configs from Vigilance
Powered by GitBook
On this page
  • Getting a string response from a URL
  • Getting a JSON response from a URL
  • Downloading files
  • Opening a link in the user's default browser
Export as PDF
  1. Utilities Module

Networking utilities

There is a small set of basic, yet useful, networking utilities for things such as obtaining a string from a URL, downloading a file, etc.

Getting a string response from a URL

String url = "https://example.com";
String userAgent = "Example/1.0.0";
int timeout = 30_000; // 30 seconds
boolean useCaches = true; // If we request from this same URL multiple times, the response will be cached

String response = NetworkUtils.getString(url, userAgent, timeout, useCaches);
System.out.println("Response from example.com:\n" + response);
val url = "https://example.com"
val userAgent = "Example/1.0.0"
val timeout = 30_000 // 30 seconds
val useCaches = true // If we request from this same URL multiple times, the response will be cached

val response = NetworkUtils.getString(url, userAgent, timeout, useCaches)
println("Response from example.com:\n$response")

It is recommended to use the name of your mod as the first half of your user agent, and the version as the second half.

Otherwise, you can simply call getString using a URL to use the default OneConfig user agent, a timeout of 5000 milliseconds (5 seconds) and no caching.

String url = "https://example.com";

String response = NetworkUtils.getString(url);
System.out.println("Response from example.com:\n" + response);
val url = "https://example.com"

val response = NetworkUtils.getString(url)
println("Response from example.com:\n$response")

Getting a JSON response from a URL

String url = "https://example.com";
JsonElement json = JsonUtils.parseFromUrl(url);

// You can use your JsonElement as you please
val url = "https://example.com"
val json: JsonElement = JsonUtils.parseFromUrl(url)

// You can use your JsonElement as you please

Downloading files

String url = "https://example.com";
Path path = Paths.get("/path/to/your/file");
String userAgent = "Example/1.0.0";
int timeout = 30_000; // 30 seconds
boolean useCaches = true; // If we request from this same URL multiple times, the response will be cached

boolean result = NetworkUtils.downloadFile(url, path, userAgent, timeout, useCaches);
System.out.println("File download status: " + result);
val url = "https://example.com"
val path = Paths.get("/path/to/your/file")
val userAgent = "Example/1.0.0"
val timeout = 30_000 // 30 seconds
val useCaches = true // If we request from this same URL multiple times, the response will be cached

val result = NetworkUtils.downloadFile(url, path, userAgent, timeout, useCaches)
println("File download status: $result")

Again, similarly to getString, you can simply call this method with only the URL and path of the downloaded file.

String url = "https://example.com";
Path path = Paths.get("/path/to/your/file");

boolean result = NetworkUtils.downloadFile(url, path);
System.out.println("File download status: " + result);
val url = "https://example.com"
val path = Paths.get("/path/to/your/file")

val result = NetworkUtils.downloadFile(url, path)
println("File download status: $result")

Opening a link in the user's default browser

String ourUrl = "https://example.com";
NetworkUtils.browseLink(ourUrl);
PreviousIO utilitiesNextJSON utilities

Last updated 5 months ago