Java 如何按顺序进行多个API调用

Java 如何按顺序进行多个API调用,java,rest,synchronous,sequential,Java,Rest,Synchronous,Sequential,我需要调用两个API A1和A2,但不能并行调用。只有当A1在其JSON响应中返回一些标志值时,A2才会被调用 我知道如何使用Httpclient在java中进行http调用。一种方法是编写一个代码来进行第一次调用并解析其响应,然后再次使用相同的代码进行另一次调用。他们是否有其他聪明的方法来为我们自动化这个过程,我将传递请求和第二次调用的条件,就像在Rxjava中一样 以下是Rxjava代码段参考: 如何在Java中实现这一点?是否有任何Java特性已经存在,并且允许我进行多个顺序调用 我尝试搜

我需要调用两个API A1和A2,但不能并行调用。只有当A1在其JSON响应中返回一些标志值时,A2才会被调用

我知道如何使用Httpclient在java中进行http调用。一种方法是编写一个代码来进行第一次调用并解析其响应,然后再次使用相同的代码进行另一次调用。他们是否有其他聪明的方法来为我们自动化这个过程,我将传递请求和第二次调用的条件,就像在Rxjava中一样

以下是Rxjava代码段参考:

如何在Java中实现这一点?是否有任何Java特性已经存在,并且允许我进行多个顺序调用

我尝试搜索现有的解决方案,但它们不是用Java编写的。

您可以使用HttpURLConnection进行API调用

检查响应并相应地触发另一个呼叫

像这样的

public static void main(String[] args) throws IOException {

    String response1 = sendGET("http://url1");
    if(response1 != null && response1.contains("true")){
        String response2 = sendGET("http://url2");
    }

}

private static String sendGET(String url) throws IOException {
    URL obj = new URL(url);
    StringBuffer response = new StringBuffer();
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    System.out.println("GET Response Code :: " + responseCode);
    if (responseCode == HttpURLConnection.HTTP_OK) { // success
        BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        System.out.println("GET request not worked");
    }
    return response.toString();
}

如果已经存在使我们进行顺序调用的java类你是说。。。比如?这是什么意思?如果已经存在使我们进行顺序调用的java类,那么您可以不简单地拥有一段java代码,它1向A1发出调用并接收响应;2解析响应并检查适当的标志值;3根据标志值,调用A2或不调用A2?为了更清楚,我对问题进行了编辑。请看一看,谢谢!请再次检查我的问题。我已编辑以使其更清楚。谢谢你的回答!!!
public static void main(String[] args) throws IOException {

    String response1 = sendGET("http://url1");
    if(response1 != null && response1.contains("true")){
        String response2 = sendGET("http://url2");
    }

}

private static String sendGET(String url) throws IOException {
    URL obj = new URL(url);
    StringBuffer response = new StringBuffer();
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    System.out.println("GET Response Code :: " + responseCode);
    if (responseCode == HttpURLConnection.HTTP_OK) { // success
        BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        System.out.println("GET request not worked");
    }
    return response.toString();
}