我在使用JGit客户端时遇到了一些挑战。我将其嵌入到Java应用程序中,希望获取Github账户下的所有存储库并显示它们。此外,我可以使用JGit直接在Github上创建存储库吗?像创建远程存储库这样的操作?
我已经阅读了这个链接link,但是它对我来说似乎太通用了。提前感谢。
我在使用JGit客户端时遇到了一些挑战。我将其嵌入到Java应用程序中,希望获取Github账户下的所有存储库并显示它们。此外,我可以使用JGit直接在Github上创建存储库吗?像创建远程存储库这样的操作?
我已经阅读了这个链接link,但是它对我来说似乎太通用了。提前感谢。
GET /users/:username/repos
一个使用Java库进行这些调用的例子是"GitHub API for Java",以及它的java.org.kohsuke.github.GHPerson.java#listRepositories()
方法。
new PagedIterator<GHRepository>(
root.retrieve().asIterator("/users/" + login + "/repos?per_page=" + pageSize,
GHRepository[].class, pageSize))
我也在做同样的需求,获取特定用户的存储库列表。尝试使用以下代码,你将获得该用户的所有存储库。 //这里的name表示GitHub账户的用户名
public Collection<AuthMsg> getRepos(String name) {
String url = "https://api.github.com/users/"+name+"/repos";
String data = getJSON(url);
System.out.println(data);
Type collectionType = new TypeToken<Collection<AuthMsg>>(){}.getType();
Collection<AuthMsg> enums = new Gson().fromJson(data, collectionType);
return enums;
}
//getJson方法
public String getJSON(String url) {
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (MalformedURLException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
//AuthMsg类
public class AuthMsg {
//"name" which is in json what we get from url
@SerializedName("name")
private String repository;
/**
* @return the repository
*/
public String getRepository() {
return repository;
}
/**
* @param repository the repository to set
*/
public void setRepository(String repository) {
this.repository = repository;
}
}