有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在网站上显示GitHub时间表/提要

如何获取GitHub提要/时间线并将其显示在网站中

在GitHub,我们可以看到我们最近的活动。如果需要,如何在我们的网站上展示


共 (1) 个答案

  1. # 1 楼答案

    本文档介绍如何在网站上显示GitHub提要/时间线

    注:it is not merely OK to ask and answer your own question, it is explicitly encouraged

    示例:现场示例可从以下网址获得:

    https://newtonjoshua.com

    GitHub时间线:

    GitHub以Atom格式为任何用户提供公共时间线

    您可以在以下位置查看时间线:

    https://github.com/{{GitHub_username}}.atom

    参考:
    https://developer.github.com/v3/activity/feeds

    阅读提要

    下面是解析atom提要的Java代码

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import com.google.appengine.labs.repackaged.org.json.JSONException;
    import com.google.appengine.labs.repackaged.org.json.JSONObject;
    import com.google.appengine.labs.repackaged.org.json.XML;
    
    public class Feeds {
        public static int PRETTY_PRINT_INDENT_FACTOR = 4;
    
        public static JSONObject getFeeds(String inputUrl) throws Exception {
            URL url = new URL(inputUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    
            conn.setRequestMethod("GET");
    
            String line, outputString = "";
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
            while ((line = reader.readLine()) != null) {
                outputString += line;
            }
    
            JSONObject xmlJSONObj = null;
    
            try {
                xmlJSONObj = XML.toJSONObject(outputString);
                String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
            } catch (JSONException je) {
                System.out.println(je.toString());
            }
            return xmlJSONObj;  
        }
    }
    

    使用上面的提要解析,阅读atom提要,然后可以在网站上格式化和显示GitHub提要/时间线