in the java search_Java Github repository search

I am making a Java based CLI application in which i can search Github repository and their latest tags. For this I built an program which calls Github API and captures the highest stars repository name and its owner. This functionality is properly working when you run below code it returns "cbeust".

My second step is to capture the latest tags using owner and repository name(url2) but when I call my program to capture it does run but does not produce expected output.

Please look at my program and let me know what i am doing wrong?

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 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 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();

}

}

Current Output

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

Owner: (cbeust)

Expected Output

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

Owner: (cbeust)

first tag: (testng-6.9.5)

JSON response for the tags request

[

{

"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=="

},

{

...

}

]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值