Simplest Way of Calling API using Java
Simplest Way of Calling API using Java

In this video, we are going to call API using a java programming language. we are going to use java 11 and HTTPCLIENT and HTTPREQUEST simplest way of calling API using java .


APIMain.java
package com.api;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


public class APIMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        var url = "https://covid19.mathdro.id/api";
        //httprequest
        var request = HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
        var client = HttpClient.newBuilder().build();
        var response = client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.statusCode());
        System.out.println(response.body());

    }

}