Skip to content

Instantly share code, notes, and snippets.

@stellamiranda
Created December 18, 2023 16:27
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 stellamiranda/df28e29a9e7fedac97573e17150af1a4 to your computer and use it in GitHub Desktop.
Save stellamiranda/df28e29a9e7fedac97573e17150af1a4 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
String url = "https://api.myjson.com/bins/1h3vb3"; // replace with your URL
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
//parse JSON
JSONObject myResponse = new JSONObject(response.toString());
//print JSON
System.out.println(myResponse);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment