Retrofit是一个当前很流行的网络请求库, 官网的介绍是: "Type-safe HTTP client for Android and Java". 本文介绍Retrofit的使用.
先介绍单独使用Retrofit进行网络请求, 后面主要介绍和RxJava结合的请求, 有实例代码.

Retrofit单独使用

Setup

首先在manifest中加上网络权限:

<uses-permission android:name="android.permission.INTERNET" />

然后在app/build.gradle中加上依赖:

compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.google.code.gson:gson:2.8.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0'

准备API和model类

本例子中使用Github API做请求.

以Github的Root Endpoint为例:
https://api.github.com.
首先, 我们在命令行发送:

网友评论