0
 try {
        String apikey = "-------";
        String url = "https://freecurrencyapi.net/api/v2/latest?apikey=" + apikey + "&base_currency=USD";
        URL urlForGetRequest = new URL(url);
        String readLine = null;
        HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();
        conection.setRequestMethod("GET");
        int responseCode = conection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(conection.getInputStream()));
            StringBuffer response = new StringBuffer();
            while ((readLine = in.readLine()) != null) {
                response.append(readLine);
            }
            in.close();
            System.out.println(response.toString());
        } else {
            throw new Exception("Error in API Call");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

How I can save values from api to Hashmap List? Where key will be first worth (e.g "JPY") and value will be worth of "JPY" (E.G 115). I wanted to use Jackson lib, but I didn't find any information for how to do it. enter image description here

VLAZ
  • 26,331
  • 9
  • 49
  • 67
snoocky
  • 3
  • 6

3 Answers3

0

What you're describing is caching. There are quite a few libraries to handle this, I would recommend EHCache, but I'm sure newer libraries have sprung up since last I did this kind of work. You should be using a framework to facilitate web calls. If you execute your calls from Spring, there are a set of annotations you can use that will do the caching for you behind the scenes.

Ryan
  • 1,762
  • 6
  • 11
  • guess he dosent want to cache anything and the requirement is just saving some values extracted from result . – Lunatic Feb 15 '22 at 17:51
0

Instead of having buffer reader consider using RestTemplate i,e

RestTemplate restTemplate = new RestTemplate();
String yurDestinationUrl= "http://blablalba";
ResponseEntity<String> response
  = restTemplate.getForEntity(yurDestinationUrl + "/1", String.class);

ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(response.getBody());
JsonNode name = root.path("name");

//then add extracted JsonNode to your desired map or list as you prefer
Lunatic
  • 1,519
  • 8
  • 24
0

You can create a POJO (Plain Old Java Object) from the json response that you are dealing with.

There is an IntelliJ plugin which called RoboPOJOGenerator or by other websites which you can easily find with this search json to pojo

Under the context menu's new option on any project package.

Or you can create that POJO manually.

After creating this class you should create gson from json string like below:

Gson gson = new Gson();

// JSON string to Java object
Currencies currencies = gson.fromJson(response.toString(), Currencies.class);

Finally you have a meaningful object instance which you can use/manipulate easily as you wish.

Ozan
  • 85
  • 1
  • 5