Reputation: 192
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.
Upvotes: 0
Views: 1691
Reputation: 1496
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:
Upvotes: 2