Alfie
Alfie

Reputation: 13

How to access and show an image from an API (URL)?

I am trying to access pixabay images with this URL: "https://pixabay.com/api/?key=14291199-65ce36d5f7ccc529de84c3b84". I'm having trouble manipulating json data from this URL and displaying images. What I would do is access the API URL images and show them as a group of images displayed next to each other.

Upvotes: 1

Views: 668

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

Use something like:

Rest.get("https://pixabay.com/api/").
     jsonContent().
     queryParam("key", MY_API_KEY).
     fetchAsJsonMap(response -> {
         Map data = response.getResponseData();
         List<String, Map> hits = (List<String, Map>)data.get("hits");
         for(Map hit : hits) {
             String url = (String)hit.get("previewURL");
             addPreviewURL(url);
         }
     });

You can also use properties which is even more powerful https://www.codenameone.com/blog/properties-are-amazing.html

Upvotes: 1

Related Questions