You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Launchpad API获取archive.ubuntu.com中的所有Ubuntu软件包列表(Java实现)

如何通过Launchpad API获取archive.ubuntu.com中的所有Ubuntu软件包列表(Java实现)

嗨,我来帮你解决这个问题!你之前调用的那个端点不对,所以才返回null,咱们一步步来搞定用Java通过Launchpad API获取Ubuntu软件包列表的需求。

一、先纠正API端点的问题

你之前用的https://api.launchpad.net/devel/archives?ws.op=getByReference&reference=ubuntu/bionic其实是用来查找第三方归档(比如PPA)的,并不适用于官方Ubuntu发行版的主归档。正确的做法是直接定位到对应发行版的归档端点,比如针对bionic版本,正确的基础端点是:
https://api.launchpad.net/devel/ubuntu/bionic/archive

二、获取软件包列表的核心流程

Launchpad API提供了专门的方法来获取归档里的包列表:

  • getPublishedSources:获取源码包列表
  • getPublishedBinaries:获取二进制包列表

这两个方法支持分页(通过ws.size参数控制每页返回数量,最大可以设为100),因为Ubuntu的包数量非常多,必须处理分页逻辑才能拿到完整列表。

三、Java实现的具体步骤

1. 准备依赖

我们可以用OkHttp作为HTTP客户端,Jackson来解析JSON响应,以Maven为例,在pom.xml里添加以下依赖:

<dependencies>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.12.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.15.2</version>
    </dependency>
</dependencies>

2. 编写代码实现

下面是一个简单的示例,用来获取bionic版本的所有源码包列表:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;

public class LaunchpadPackageFetcher {
    private static final OkHttpClient client = new OkHttpClient();
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws IOException {
        // 初始请求地址,ws.size设为100以减少分页次数
        String currentUrl = "https://api.launchpad.net/devel/ubuntu/bionic/archive/getPublishedSources?ws.size=100";
        
        // 循环处理分页,直到没有下一页
        while (currentUrl != null) {
            Request request = new Request.Builder().url(currentUrl).build();
            try (Response response = client.newCall(request).execute()) {
                if (!response.isSuccessful()) {
                    throw new IOException("请求失败,状态码: " + response.code());
                }
                
                // 解析JSON响应
                JsonNode rootNode = objectMapper.readTree(response.body().string());
                JsonNode packageEntries = rootNode.get("entries");
                
                // 遍历并打印包信息
                for (JsonNode entry : packageEntries) {
                    String pkgName = entry.get("source_package_name").asText();
                    String pkgVersion = entry.get("source_package_version").asText();
                    System.out.printf("软件包: %s | 版本: %s%n", pkgName, pkgVersion);
                }
                
                // 获取下一页的链接,没有的话就结束循环
                currentUrl = rootNode.has("next_collection_link") 
                    ? rootNode.get("next_collection_link").asText() 
                    : null;
            }
        }
        System.out.println("所有软件包已获取完成!");
    }
}

3. 一些注意事项

  • 如果需要获取二进制包,只需要把请求地址里的getPublishedSources换成getPublishedBinaries即可。
  • Launchpad API有速率限制,不要短时间内发送大量请求,避免被限流。
  • 如果需要更详细的包信息,可以从返回的JSON节点里提取更多字段,比如维护者、发布时间等。

备注:内容来源于stack exchange,提问作者premdeep sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 07:32:57