SoFunction
Updated on 2025-03-02

A detailed guide to several methods of calling third-party interfaces in Java

Preface

In Java development, it is often necessary to call third-party interfaces to obtain data or execute certain services. This article will introduce in detail how to use Java to call third-party interfaces, including using JDK's ownHttpURLConnectionClasses and popular third-party libraries such as OkHttp and Apache HttpClient.

1. Use HttpURLConnection to call third-party interfaces

1. GET request example

import ;
import ;
import ;
import ;

public class ThirdPartyApiCaller {

    public static void main(String[] args) {
        try {
            // Create URL object            URL url = new URL("/data");

            // Create HttpURLConnection object            HttpURLConnection connection = (HttpURLConnection) ();

            // Set the request method to GET            ("GET");

            // Send a request and get the response code            int responseCode = ();

            // Check the response code            if (responseCode == HttpURLConnection.HTTP_OK) {
                // Read the response content                BufferedReader reader = new BufferedReader(new InputStreamReader(()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = ()) != null) {
                    (line);
                }
                ();

                // Process the response content                (());
            } else {
                // Handle error response                ("Request failed, response code: " + responseCode);
            }

            // Close the connection            ();

        } catch (Exception e) {
            ();
        }
    }
}

2. POST request example

import ;
import ;
import ;
import ;
import ;
import ;

public class ThirdPartyApiCaller {

    public static void main(String[] args) {
        try {
            // Create URL object            URL url = new URL("/submit");

            // Create HttpURLConnection object            HttpURLConnection connection = (HttpURLConnection) ();

            // Set the request method to POST            ("POST");

            // Set request header            ("Content-Type", "application/json");

            // Enable output stream            (true);

            // Create a request body            String requestBody = "{\"key\":\"value\"}";

            // Write the request body to the output stream            DataOutputStream outputStream = new DataOutputStream(());
            ((StandardCharsets.UTF_8));
            ();
            ();

            // Send a request and get the response code            int responseCode = ();

            // Check the response code            if (responseCode == HttpURLConnection.HTTP_OK) {
                // Read the response content                BufferedReader reader = new BufferedReader(new InputStreamReader(()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = ()) != null) {
                    (line);
                }
                ();

                // Process the response content                (());
            } else {
                // Handle error response                ("Request failed, response code: " + responseCode);
            }

            // Close the connection            ();

        } catch (Exception e) {
            ();
        }
    }
}

2. Use the OkHttp library to call third-party interfaces

OkHttp is a powerful HTTP client library for sending and receiving HTTP requests. Here is an example of how to send a POST request using OkHttp:

import okhttp3.*;
import ;

public class OkHttpExample {

    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();

        String url = "/submit";
        String json = "{\"key\":\"value\"}";

        MediaType JSON = ("application/json; charset=utf-8");
        RequestBody body = (json, JSON);

Summarize

This is the end of this article about several methods of calling third-party interfaces in Java. For more related content on calling third-party interfaces, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!