java实现接口访问(一)

来源:互联网 发布:appuploader windows 编辑:程序博客网 时间:2024/06/06 07:06

一、前置准备:

  1. PostMan
  2. 访问的网站:Github
  3. 访问的接口:
    https://api.github.com/search/commits?q=committer-date:2017-11-27..2017-12-01&page=1&per_page=100
  4. 实现访问:查找2017-11-27到 2017-12-01的100条数据

二、代码实现
1. 使用PostMan输入访问的接口,取得需要的字段,items,和items中repository的字段id,和full_name(可以自己获取想要的字段)
这里写图片描述

  1. java类
    (1)、CommitRepo
public class CommitRepo {    private String id;    private String full_name;    @Override    public String toString() {        return "CommitRepo [id=" + id + ", full_name=" + full_name + "]";    }    public String getId() {        return id;    }    public String getFull_name() {        return full_name;    }}

(2)、CommitItems

public class CommitItems {    private CommitRepo repository;//名字要一样(repository)    public CommitRepo getRepository() {        return repository;    }    @Override    public String toString() {        return "CommitItems [repository=" + repository + "]";    }}

(3)、GithubCommitsResult

public class GithubCommitsResult {    private CommitItems[] items;    public CommitItems[] getItems() {        return items;    }    @Override    public String toString() {        return "GithubCommitsResult [items=" + Arrays.toString(items) + "]";    }}

(4)、GithubTest测试类

public static void main(String[] args) {    try {    URL url = new URL("https://api.github.com/search/commits?q=committer-date:2017-11-27..2017-12-01&page=10&per_page=100");    HttpsURLConnection https = (HttpsURLConnection) url.openConnection();// 打开连接                https.addRequestProperty("Accept","application/vnd.github.cloak-preview");// 设置请求属性    https.addRequestProperty("Authorization", "Basic "+up);    https.setDoInput(true);    InputStream is = https.getInputStream();    Gson gson = new GsonBuilder().create();    GithubCommitsResult gcr = gson.fromJson(new InputStreamReader(is),GithubCommitsResult.class);    for(int i=0;i<gcr.getItems().length;i++){          System.out.println(gcr.getItems()[i].getRepository().getId());    }        is.close();        https.disconnect();    } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

(5)、GithubTest测试类代码修改

public static void main(String[] args) {    String url="https://api.github.com/search/commits?q=committer-date:2017-11-27..2017-12-01&page=1&per_page=100";    String ret=getGithubApiReply(url);    Gson gson = new GsonBuilder().create();    GithubCommitsResult gcr = gson.fromJson(ret,GithubCommitsResult.class);    System.out.println(gcr.getItems()[99].getRepository().getId());}
public static String getGithubApiReply(String api_url) {    String retstr = null;    try {        URL url = new URL(api_url);        HttpsURLConnection https = (HttpsURLConnection) url                    .openConnection();// 打开连接        https.addRequestProperty("Accept",                    "application/vnd.github.cloak-preview");// 设置请求属性               https.setDoInput(true);        InputStream is = https.getInputStream();        Reader reader = new InputStreamReader(is, "utf-8");        StringBuffer sb = new StringBuffer();        int c = -1;        while ((c = reader.read()) != -1) {            sb.append((char) c);        }        retstr = new String(sb);        is.close();        https.disconnect();    } catch (IOException e) {        e.printStackTrace();    }    return retstr;}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 楷书书法欣赏 中国书法在线网 中国书法网首页 书法图片欣赏 王羲之书法欣赏 书法作品欣赏楷书 颜真卿书法欣赏 四字书法图片 书法欣赏高清图片 行书书法大全 钢笔书法学习 书法字体在线转换 书法字体下载 少儿书法培训班 怎么练毛笔书法 专业书法培训 硬笔书法 培训 老年书法培训班 毛笔书法学习 毛笔书法培训班 毛笔书法培训 毛笔书法考级 少儿硬笔书法班 隶书书法入门 书法自学入门 硬笔书法学习 名家书法作品图片 怎样练毛笔书法 钢笔书法入门 书法在线生成 怎么练硬笔书法 书法培训多少钱 初学书法用什么毛笔 毛笔书法教案 硬笔书法学习班 王羲之书法拍卖价格 书法名家作品 少儿毛笔书法 小学生书法教学 怎么练好书法 零基础学书法