Created
December 18, 2023 16:27
-
-
Save stellamiranda/df28e29a9e7fedac97573e17150af1a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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