0

I am dealing with a pretty old API provided from KhanAcademy.

Link to git: https://github.com/Khan/khan-api/wiki/Khan-Academy-API

I don't have any experience consuming REST services or how to parse Json from them. I have tried what I could find online but most SO posts or other things on the internet don't involve handling both REST and json at the same time. I have tried mapping the json to a map but I couldn't get that to work due to my Json queries not being processed properly.

Here are some bits of code that I have tried using:

public static Object getConnection(String url){
    URL jsonUrl;
    String message;
    try{
        jsonUrl = new URL(url);
        System.out.println("This is the URL: "+ jsonUrl);
    } catch (MalformedURLException ex) {
        message = "failed to open a new conenction. "+ ex.getMessage();
        //logger.warn(message);
        throw new RuntimeException();
    }
    URLConnection connection;
    try{
        connection = jsonUrl.openConnection();
        connection.connect();
    }catch(IOException e){
        message = "Failed to open a new connection. " + e.getMessage();
        //logger.warn(message);
        throw new RuntimeException();
    }


    Object jsonContents;
    try{
        jsonContents = connection.getContent();


        System.out.println("This is the content: "+jsonContents);
    }catch(IOException e){
        message = "failed to get contents.";
        //logger.warn(message);
        throw new RuntimeException(message);
    }
    return jsonContents;
    }

the below is using JAX RS API

    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://www.khanacademy.org/api/v1/topictree");
    JsonArray response = target.request(MediaType.APPLICATION_JSON).get(JsonArray.class);

}

The below is some "Zombie code", it is a compilation of things I have tried mainly shown to verify that I am truly lost and that I have been looking for a solution for about 7 hours now?


    JsonReader reader = new JsonReader(response);
    JsonParser parser = new JsonParser();

    JsonElement rootElement = parser.parse(reader);
    JsonElement rootElement = parser.parse(response.getAsString());
    JsonArray jsonArray = rootElement.getAsJsonArray();
    ArrayList results = new ArrayList();
    Gson myGson = new Gson();
    for(JsonElement resElement : jsonArray){
       //String mp4 = myGson.fromJson(resElement, );
   }
    JsonArray jarray =jsonObject.getAsJsonArray();

    jsonObject= jarray.get(0).getAsJsonObject();
    String result = jsonObject.get("title").getAsString();
    System.out.println(result);
    JsonObject resultObject = jsonObject.getAsJsonObject("url");
    String result = resultObject.getAsString();
    System.out.println(result);
    JsonObject jsonObject=response.get(0).getAsJsonObject();

    return new Gson().fromJson(url, mapType);
}

Any help is appreciated.

2
  • What do you want to extract from the json result after making the API call? Commented Apr 3, 2019 at 0:29
  • @Karan Hi Karan, thanks for replying. If I may show you this: [link]api-explorer.khanacademy.org/api/v1/topic[link]. I would like to get all the youtube links/ mp4 urls for a topic/subject the user chooses.I originally pointed my browser at [link]khanacademy.org/api/v1/topic[link] /<topic name>. Issue probably lies with me in not knowing what to do with a parsed json. I also found that searching by topic often meant there was no result so instead I tried pointing at this: [link]khanacademy.org/api/v1/topictree[link] Commented Apr 3, 2019 at 5:39

1 Answer 1

2

You can use Feign to do it.

I recommend you create Java classes for representing the JSON structure, which is defined here

Here a basic demo:

public class App {
    public static void main(String[] args) {
        KhanAcademyAPI khanAcademyAPI = Feign.builder()
                .decoder(new GsonDecoder())
                .logLevel(Logger.Level.HEADERS)
                .target(KhanAcademyAPI.class, "http://www.khanacademy.org");


        Topic root = khanAcademyAPI.tree();
        root.children.forEach(topic1 -> System.out.println(topic1.slug));

        Topic science = khanAcademyAPI.topic("science");
        science.children.forEach(topic1 -> System.out.println(topic1.description));

    }

    public static class Topic {
        String description;
        boolean hide;
        String slug;
        List<Topic> children;
    }

    interface KhanAcademyAPI {
        @RequestLine("GET /api/v1/topictree")
        Topic tree();

        @RequestLine("GET /api/v1/topic/{topic_slug}")
        Topic topic(@Param("topic_slug") String slug);
    }
}

I used the following maven dependencies only:

  • io.github.openfeign:feign-core:10.2.0
  • io.github.openfeign:feign-gson:10.2.0
Sign up to request clarification or add additional context in comments.

1 Comment

You are a thing of beauty. Thank you so much. I am surprised to not see this anywhere else on the internet without searching for Feign specifically. You can't imagine the troubles I have had with trying to work with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.