Skip to content

Instantly share code, notes, and snippets.

@InventivetalentDev
Created December 27, 2016 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InventivetalentDev/adacaa1e230e6a21da1e998551a07586 to your computer and use it in GitHub Desktop.
Save InventivetalentDev/adacaa1e230e6a21da1e998551a07586 to your computer and use it in GitHub Desktop.
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class JSONAPI {
private static final String USER_AGENT = "MyUserAgent";// Change this!
private static final String REQUEST_URL = "https://api.spiget.org/v2/resources/2";
public static void main(String[] args) {
try {
URL url = new URL(REQUEST_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", USER_AGENT);// Set User-Agent
// If you're not sure if the request will be successful,
// you need to check the response code and use #getErrorStream if it returned an error code
InputStream inputStream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
// This could be either a JSONArray or JSONObject
Object value = JSONValue.parseWithException(reader);
// TODO: process value
System.out.println(value);
} catch (IOException | ParseException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment