-2

I am trying to run an Api request from yahoo Finance but something is not right. I am getting a java: unreported exception java.io.IOExceotion error.

This is the current requerest:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {

    public static void main(String[] args) {


        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-chart?interval=5m&symbol=AMRN&range=1d&region=US"))
                .header("x-rapidapi-key", "72d781e00bmsh5b6e67e1b515e76p1c53a1jsn0c19b3ed2e1a")
                .header("x-rapidapi-host", "apidojo-yahoo-finance-v1.p.rapidapi.com")
                .method("GET", HttpRequest.BodyPublishers.noBody())
                .build();
        HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());

    }
}

This is the error I am getting:

C:\Users\L\Documents\CS\PROJECTSPRINGBOOT\WebHel\src\com\company\Main.java:18:72
java: unreported exception java.io.IOException; must be caught or declared to be thrown

How can I solve this problem? I am using rapidAPI.

Leonardo
  • 49
  • 3
  • read about try & catch – riorio Jan 13 '21 at 18:17
  • the solutions are in the error – mre Jan 13 '21 at 18:19
  • 3
    Does this answer your question? [Error message "unreported exception java.io.IOException; must be caught or declared to be thrown"](https://stackoverflow.com/questions/8707906/error-message-unreported-exception-java-io-ioexception-must-be-caught-or-decla) – Karol Katanowski Jan 13 '21 at 18:22

2 Answers2

0

You need to add throws java.io.IOException to the main method signature.

public static void main(String[] args) throws IOException {

Also this question is duplicated. Here is the link Error message "unreported exception java.io.IOException; must be caught or declared to be thrown"

Karol Katanowski
  • 176
  • 2
  • 13
0

You should use Java Exceptions...

Try this:

public class Main {

    public static void main(String[] args) throws IOException {


        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-chart?interval=5m&symbol=AMRN&range=1d&region=US"))
                .header("x-rapidapi-key", "72d781e00bmsh5b6e67e1b515e76p1c53a1jsn0c19b3ed2e1a")
                .header("x-rapidapi-host", "apidojo-yahoo-finance-v1.p.rapidapi.com")
                .method("GET", HttpRequest.BodyPublishers.noBody())
                .build();
        HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());

    }
}
Hubi
  • 440
  • 1
  • 11
  • 25