【问题标题】:Github Java API - search reposGithub Java API - 搜索存储库
【发布时间】:2015-07-28 03:18:02
【问题描述】:

我想根据大小和关键字参数搜索 Github 存储库。这是我写的Java代码:

package searchTest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.egit.github.core.SearchRepository;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.RepositoryService;

public class GithubSearch {
    private static Map<String, String> searchQuery = new HashMap<String, String>();

    public static void main(String[] args) throws IOException {
        GitHubClient client = new GitHubClient();
        client.setCredentials("username", "password");
        RepositoryService service = new RepositoryService(client);
        searchQuery.put("keyword","microsoft"); 
        searchQuery.put("size","304");
        List<SearchRepository> searchRes = service.searchRepositories(searchQuery);
        System.out.println("Search result "+searchRes.toString());
    }   
}

但是我得到的输出是空的:

Search result []

我检查了GitHub Java API javadoc,但找不到解决方案。

谢谢!

【问题讨论】:

    标签: github-api


    【解决方案1】:

    试试jcabi-github(我是它的开发者之一):

    final Github github = new RtGithub();
    final JsonResponse resp = github.entry()
        .uri().path("/search/repositories")
        .queryParam("q", "java").back()
        .fetch()
        .as(JsonResponse.class);
    final List<JsonObject> items = resp.json().readObject()
        .getJsonArray("items")
        .getValuesAs(JsonObject.class);
    for (final JsonObject item : items) {
        System.out.println(
            String.format(
                "repository found: %s",
                item.get("full_name").toString()
            )
        );
    }
    

    完整示例在这里:https://github.com/jcabi/jcabi-github/tree/master/examples/search-repos

    【讨论】:

      【解决方案2】:

      我运行了如下示例代码,没有关键字“java”的大小

      Map<String, String> searchQuery = new HashMap<String, String>();
      searchQuery.put("keyword", "java");
      List<SearchRepository> searchRes = null;
      try {
          searchRes = service.searchRepositories(searchQuery);
      } catch (IOException e) {
          e.printStackTrace();
      }
      
      System.out.println("Search result "+searchRes.toString());
      

      输出如下:

          Search result [michaelriha/WASC-Essay, sayems/Keyword-Driven-Framework, youdevise/karg, frinknet/DBG.js, andorp/testkeys, jpolitz/defaults.js, lsmith/article--javascript-this, qiaoyu314/TwitterMiner, cookieglitch/SearchToDB_Twitter, snoooze03/costOfVolatile, dziaduZLasu/java-critical-section-synchro, tvalletta/this-presentation, youdevise/jpoeisis, Sixtease/RtkInputMethod, metabrain/java8-plugin-persitent-local-vars, atestology/roark, jsmreese/pureshare-javascript-api, kentcdodds/genie, TARUNTEJAKOPPULA/Runnung-java-code-with-another-java-program,  aleSuglia/SitesLook, neetsdkasu/LittleLisp, odbol/Port-Java-to-C, artfulgeek/Hybrid-Search-and-Delivery-of-Learning-Objects, itcuties/Java-XStream-Complex-Example, codeheadzxc/TomsMovieFinder, truongsinh/generator, nsdevaraj/SwizDAO, dineshkummarc/jameleon-test-suite-3.3-RC1]
      

      现在添加大小 196 到查询搜索:

      Map<String, String> searchQuery = new HashMap<String, String>();
      searchQuery.put("keyword", "java");
      searchQuery.put("size", "196");
      List<SearchRepository> searchRes = null;
      try {
          searchRes = service.searchRepositories(searchQuery);
      } catch (IOException e) {
          e.printStackTrace();
      }
      System.out.println("Search result "+searchRes.toString());
      

      输出如下:

          Search result [michaelriha/WASC-Essay]
      

      我认为“大小”参数不是固定搜索限制的数量,而是仅获取具有该大小的存储库。

      【讨论】:

        【解决方案3】:

        也许您应该尝试仅搜索关键字。我认为他们不支持尺寸搜索。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-25
          • 2022-11-11
          • 2017-12-08
          相关资源
          最近更新 更多