Answered step by step
Verified Expert Solution
Question
1 Approved Answer
when I run the program my api fails can you explain and fix the code than you import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
when I run the program my api fails can you explain and fix the code than you
import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Scanner; public class Main { private static final String APIKEY = "deOgBms2347e9HGCO7QF2pdC0GVIjuOI" ; public static void main(String[] args) throws IOException { HashMapcurrencyCodes = new HashMap (); currencyCodes.put(1, "USD"); currencyCodes.put(2, "CAD"); currencyCodes.put(3, "EUR"); currencyCodes.put(4, "HKD"); currencyCodes.put(5, "INR"); String fromCode, toCode; double amount; Scanner sc = new Scanner(System.in); System.out.println("Welcome to the currency converter"); System.out.println("Currency converting FROM?"); System.out.println("1: USD (US Dollar)\t 2:CAD (Canadian Dollar)\t 3:EUR (EURO) 4:HKD (Hong Kong)\t 5: INR(Indian Rupees)"); fromCode = currencyCodes.get(sc.nextInt()); System.out.println("Currency converting TO?"); System.out.println("1: USD (US Dollar)\t 2:CAD (Canadian Dollar)\t 3:EUR (EURO) 4:HKD (Hong Kong)\t 5: INR(Indian Rupees)"); toCode = currencyCodes.get(sc.nextInt()); System.out.println("Amount you wish to convert?"); amount = sc.nextFloat(); sendHttpGETRequest(fromCode, toCode, amount); System.out.println("Thank you for using the currency converter!"); } private static void sendHttpGETRequest(String fromCode, String toCode, double amount) throws IOException { String GET_URL = "https://api.apilayer.com/exchangerates_data/live?base=" + toCode + "&symbols=" + fromCode; URL url = new URL(GET_URL); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setRequestProperty("deOgBms2347e9HGCO7QF2pdC0GVIjuOI", APIKEY); int responseCode = httpURLConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); String inputline; StringBuffer response = new StringBuffer(); while ((inputline = in.readLine()) != null) { response.append(inputline); }in.close(); JSONObject obj = new JSONObject(response.toString()); Double exchangeRate = obj.getJSONObject("rates").getDouble(fromCode); System.out.println(obj.getJSONObject("rates")); System.out.println(exchangeRate); System.out.println(); System.out.println(amount + fromCode + "=" + amount/exchangeRate + toCode); } else { System.out.println("Get a request failed!"); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started