Android网络库:Retrofit 使用

前言

本文记录一下,关于Android平台Retrofit网络库的使用。
通过网络上大家对不同框架的对比,我发现Retrofit是比较流行的。下面介绍一下简单的使用。
这个库对于使用json等较新的数据格式交互的应用有比较大的优势,可以和ios平台的afnetworking相提并论。

准备访问的api

简单的介绍一下,如何通过Retrofit获取我们的github仓库信息。
我们可以先访问一下github提供的api,看一下返回数据的格式和类型。

地址:https://api.github.com/users/shutup/repos
您也可以将地址中的shutup改为您的github用户名

返回数据(已一个item为例):
{
    "id": 40000107,
    "name": "AllHookInOne",
    "full_name": "shutup/AllHookInOne",
    "owner": {
      "login": "shutup",
      "id": 3433620,
      "avatar_url": "https://avatars.githubusercontent.com/u/3433620?v=3",
      "gravatar_id": "",
      "url": "https://api.github.com/users/shutup",
      "html_url": "https://github.com/shutup",
      "followers_url": "https://api.github.com/users/shutup/followers",
      "following_url": "https://api.github.com/users/shutup/following{/other_user}",
      "gists_url": "https://api.github.com/users/shutup/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/shutup/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/shutup/subscriptions",
      "organizations_url": "https://api.github.com/users/shutup/orgs",
      "repos_url": "https://api.github.com/users/shutup/repos",
      "events_url": "https://api.github.com/users/shutup/events{/privacy}",
      "received_events_url": "https://api.github.com/users/shutup/received_events",
      "type": "User",
      "site_admin": false
    },
    "private": false,
    "html_url": "https://github.com/shutup/AllHookInOne",
    "description": "",
    "fork": true,
    "url": "https://api.github.com/repos/shutup/AllHookInOne",
    "forks_url": "https://api.github.com/repos/shutup/AllHookInOne/forks",
    "keys_url": "https://api.github.com/repos/shutup/AllHookInOne/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/shutup/AllHookInOne/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/shutup/AllHookInOne/teams",
    "hooks_url": "https://api.github.com/repos/shutup/AllHookInOne/hooks",
    "issue_events_url": "https://api.github.com/repos/shutup/AllHookInOne/issues/events{/number}",
    "events_url": "https://api.github.com/repos/shutup/AllHookInOne/events",
    "assignees_url": "https://api.github.com/repos/shutup/AllHookInOne/assignees{/user}",
    "branches_url": "https://api.github.com/repos/shutup/AllHookInOne/branches{/branch}",
    "tags_url": "https://api.github.com/repos/shutup/AllHookInOne/tags",
    "blobs_url": "https://api.github.com/repos/shutup/AllHookInOne/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/shutup/AllHookInOne/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/shutup/AllHookInOne/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/shutup/AllHookInOne/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/shutup/AllHookInOne/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/shutup/AllHookInOne/languages",
    "stargazers_url": "https://api.github.com/repos/shutup/AllHookInOne/stargazers",
    "contributors_url": "https://api.github.com/repos/shutup/AllHookInOne/contributors",
    "subscribers_url": "https://api.github.com/repos/shutup/AllHookInOne/subscribers",
    "subscription_url": "https://api.github.com/repos/shutup/AllHookInOne/subscription",
    "commits_url": "https://api.github.com/repos/shutup/AllHookInOne/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/shutup/AllHookInOne/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/shutup/AllHookInOne/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/shutup/AllHookInOne/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/shutup/AllHookInOne/contents/{+path}",
    "compare_url": "https://api.github.com/repos/shutup/AllHookInOne/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/shutup/AllHookInOne/merges",
    "archive_url": "https://api.github.com/repos/shutup/AllHookInOne/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/shutup/AllHookInOne/downloads",
    "issues_url": "https://api.github.com/repos/shutup/AllHookInOne/issues{/number}",
    "pulls_url": "https://api.github.com/repos/shutup/AllHookInOne/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/shutup/AllHookInOne/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/shutup/AllHookInOne/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/shutup/AllHookInOne/labels{/name}",
    "releases_url": "https://api.github.com/repos/shutup/AllHookInOne/releases{/id}",
    "deployments_url": "https://api.github.com/repos/shutup/AllHookInOne/deployments",
    "created_at": "2015-07-31T10:24:22Z",
    "updated_at": "2015-07-31T10:24:23Z",
    "pushed_at": "2015-04-21T16:34:40Z",
    "git_url": "git://github.com/shutup/AllHookInOne.git",
    "ssh_url": "[email protected]:shutup/AllHookInOne.git",
    "clone_url": "https://github.com/shutup/AllHookInOne.git",
    "svn_url": "https://github.com/shutup/AllHookInOne",
    "homepage": null,
    "size": 2911,
    "stargazers_count": 0,
    "watchers_count": 0,
    "language": "C++",
    "has_issues": false,
    "has_downloads": true,
    "has_wiki": true,
    "has_pages": false,
    "forks_count": 0,
    "mirror_url": null,
    "open_issues_count": 0,
    "forks": 0,
    "open_issues": 0,
    "watchers": 0,
    "default_branch": "master"
  }```
内容很多,但我们仔细一看就会发现,这个api返回的数据是以json格式编码的,整个结果是一个数组,数组的每一项为一个仓库的详细信息。了解了这些信息,我们就可以开始尝试着使用retrofit库了。

#开始编码
我们使用android studio作为开发环境。
首先我们新建一个工程,名字自己起。
1.在gradle脚本中添加依赖的库

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

2.接着我们新建一个接口
```java
public interface GitHubService {    
  @GET("/users/{user}/repos")    
  Call> listRepos(@Path("user") String user);  
  }

Retrofit中将http api转化为java接口,简单介绍一下上面的代码:
注解@GET表明这是一个get请求,其中的参数为路由的相对路径,这个相对路径在发起请求时会和服务器的根路径拼接成完整的路由地址。
注解参数中的{user}会被listRepos方法的参数user进行替换,前提是listRepos方法的参数由注解@Path修饰,且@Path的参数和{}中的参数名称一致。

3.model的编写
只需要根据json对象的键值,编写model对应的成员变量,再生成get set方法就好。关于如何快速的由json数据生成model,请看这个GsonFormat插件的使用:Android Studio 插件:GsonFormat

你可能感兴趣的:(Android网络库:Retrofit 使用)