Retrofit是一個當前很流行的網(wǎng)絡請求庫, 官網(wǎng)的介紹是: "Type-safe HTTP client for Android and Java". 本文介紹Retrofit的使用.
先介紹單獨使用Retrofit進行網(wǎng)絡請求, 后面主要介紹和RxJava結合的請求, 有實例代碼.
Retrofit單獨使用
Setup
首先在manifest中加上網(wǎng)絡權限:
<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.
首先, 我們在命令行發(fā)送: