-
-
Save kwabe007/504ebe045ae700b36148d23bef06c88d to your computer and use it in GitHub Desktop.
Sample code for making a HTTP request in java
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
public static String makeHTTPRequestForLavenderTown() { | |
String urlString = "https://pokeapi.co/api/v2/location/lavender-town"; | |
String response = null; | |
try { | |
URL url = new URL(urlString); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
connection.connect(); | |
Scanner responseScanner = new Scanner(connection.getInputStream()); | |
while (responseScanner.hasNext()) { | |
response = responseScanner.nextLine(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment