Java Github 存储库搜索

Java Github repository search

我正在制作一个基于 Java 的 CLI 应用程序,我可以在其中搜索 Github 存储库及其最新标签。为此,我构建了一个程序,它调用 Github API 并捕获最高星级的存储库名称及其所有者。当您 运行 下面对其进行编码 returns "cbeust" 时,此功能可以正常工作。

我的第二步是使用所有者和存储库名称 (url2) 捕获最新标签,但是当我调用程序捕获它时 运行 但没有产生预期的输出。

请看看我的程序,让我知道我做错了什么?

package com.github_search;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import com.jayway.jsonpath.JsonPath;
import org.apache.log4j.BasicConfigurator;
import org.json.JSONException;
import org.json.JSONObject;

public class github_search
{
    public static void main(String[] args) {
        try {
            BasicConfigurator.configure();
            String Name = "TESTNG";
            String Tags = "6.4.0";
            String url1 = "https://api.github.com/search/repositories?q="+ Name +"&sort=stars&order=desc";
            List<String> authors = JsonPath.read(github_search.search(url1).toString(), "$.items[*].owner.login");
            System.out.println("Owner: (" + authors.get(0) +")");
            String url2 = "https://api.github.com/repos/"+authors.get(0)+"/"+Name+"/tags";
            List<String> latest_tags = JsonPath.read(github_search.search(url2).toString(), "$..name");
            System.out.println("first tag: (" + latest_tags.get(0) +")");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public static String search(String url) throws IOException, JSONException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        JSONObject obj_JSONObject = new JSONObject(response.toString());
        return obj_JSONObject.toString();
    }
}

当前输出

0 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath  - Evaluating path: $['items'][*]['owner']['login']
Owner: (cbeust)

预期输出

0 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath  - Evaluating path: $['items'][*]['owner']['login']
Owner: (cbeust)
first tag: (testng-6.9.5)

JSON 对标签请求的响应

[
  {
    "name": "testng-6.9.5",
    "zipball_url": "https://api.github.com/repos/cbeust/testng/zipball/testng-6.9.5",
    "tarball_url": "https://api.github.com/repos/cbeust/testng/tarball/testng-6.9.5",
    "commit": {
      "sha": "ef2d1199abff4e1b8fa4b1148c1314e776d7a044",
      "url": "https://api.github.com/repos/cbeust/testng/commits/ef2d1199abff4e1b8fa4b1148c1314e776d7a044"
    },
    "node_id": "MDM6UmVmNzQ1NzQ5OnRlc3RuZy02LjkuNQ=="
  },
  {
  ...
  }
]

这里的问题是您对 search() 的第二次调用抛出异常,您在 catch 块中吞下了异常,因此 main 方法失败并在打印出之前退出 latest_tags.get(0) 因此您只能看到此调用的输出:System.out.println(authors.get(0)).

您第一次调用 GitHub API 的响应如下所示:

{
  "total_count": 6345,
  "incomplete_results": false,
  "items": [...]
}

单个 JSON 'object' 因此此调用成功:new JSONObject(response.toString())

但是,您第二次调用 GitHub API 的响应如下所示:

[
  {
    "name": "testng-6.9.5",
    "zipball_url": "https://api.github.com/repos/cbeust/testng/zipball/testng-6.9.5",
    "tarball_url": "https://api.github.com/repos/cbeust/testng/tarball/testng-6.9.5",
    "commit": {
      "sha": "ef2d1199abff4e1b8fa4b1148c1314e776d7a044",
      "url": "https://api.github.com/repos/cbeust/testng/commits/ef2d1199abff4e1b8fa4b1148c1314e776d7a044"
    },
    "node_id": "MDM6UmVmNzQ1NzQ5OnRlc3RuZy02LjkuNQ=="
  },
  ...
]

所以,这是一个 JSON 'objects' 的数组(注意它是用方括号括起来的方式),因此这个调用:new JSONObject(response.toString()) 失败...

Value [...] of type org.json.JSONArray cannot be converted to JSONObject

所以,这就解释了为什么您只看到了第一个预期输出。现在,要解决此问题,您只需替换这些行 ...

JSONObject obj_JSONObject = new JSONObject(response.toString());
return obj_JSONObject.toString();

...有了这个:

return response.toString()

并且,为了完整起见,请在您的 main 方法的 catch 块中提供一些实现,这样您就不会被吞没的异常误导。