分享一个超级好用的java调用第三方API方法

java调用第三方API

springboot版本

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.14.RELEASE</version>
    </parent>

所需依赖

最好以下依赖都有否则可能会启动报错或者调用报错(亲测)

 			<dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>4.10.0</version>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib</artifactId>
                <version>1.8.10</version>
            </dependency>

            <!--  admin4j   -->
            <dependency>
                <groupId>io.github.admin4j</groupId>
                <artifactId>common-http-starter</artifactId>
                <version>0.4.6</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.commons</groupId>
                        <artifactId>commons-lang3</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <!--     commons-lang3       -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.12.0</version>
            </dependency>

发送请求

GET请求

普通get请求
Response response = HttpUtil.get(https://github.com/search, Pair.of(q, okhttp));
System.out.println(response =  + response);
HttpRequest 链式请求 (可以设置请求头)
Response response=HttpRequest.get(https://search.gitee.com/?skin=rec&type=repository)
.queryMap(q,admin4j)
.header(HttpHeaderKey.USER_AGENT,admin4j)
.execute(Object.class);
System.out.println(response = +response);

POST

普通POST请求
# JSON 格式的body
Response post = HttpUtil.post(https://oapi.dingtalk.com/robot/send,body);
System.out.println(post =  + post);

# form 请求
Map<String, Object> formParams = new HashMap<>(16);
formParams.put(username, admin);
formParams.put(password, admin123);
Response response = HttpUtil.postForm(http://192.168.1.13:9100/auth/login,
                formParams
);
System.out.println(response =  + response);
HttpRequest 链式请求
# post form
Response response=HttpRequest.get(http://192.168.1.13:9100/auth/login)
.queryMap(q,admin4j)
.header(HttpHeaderKey.USER_AGENT,admin4j)
.form(username,admin)
.form(password,admin123)
.execute(Object.class);
System.out.println(response = +response);
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Java 调用第三方 API 接口需要以下步骤: 1. 了解 API 文档:首先需要了解第三方 API 的使用文档,包括 API 接口地址、请求参数、返回参数等信息。 2. 创建 URL 对象:根据 API 文档中提供的接口地址,使用 Java 的 URL 类创建一个 URL 对象。 3. 打开连接:使用 URL 对象调用 openConnection() 方法打开一个连接。 4. 设置请求参数:根据 API 文档中提供的请求参数,设置请求头、请求方法、请求参数等信息。 5. 发送请求:使用连接对象的 getOutputStream() 或者 getInputStream() 方法发送请求数据或获取响应数据。 6. 解析返回结果:根据 API 文档中提供的返回参数,解析响应数据并进行处理。 以下是一个简单的 Java 调用第三方 API 的示例代码: ```java import java.net.*; import java.io.*; public class ApiTest { public static void main(String[] args) { try { // 创建 URL 对象 URL url = new URL("https://api.example.com/api/v1/user"); // 打开连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置请求参数 conn.setRequestMethod("GET"); conn.setRequestProperty("Authorization", "Bearer xxxxxxxx"); // 发送请求 InputStream inputStream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuffer response = new StringBuffer(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 解析返回结果 System.out.println(response.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 注:以上代码仅供参考,实际使用需要根据具体的 API 文档进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W先生'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值