Retrofit網路請求

2021-08-16 00:10:07 字數 1499 閱讀 3725

retrofit

一、  特點

效能最好,處理最快

使用rest api時非常方便;

傳輸層預設就使用okhttp;

支援nio;

擁有出色的api文件和社群支援

速度上比volley更快;

如果你的應用程式中整合了okhttp,retrofit缺省會使用okhttp處理其他網路層請求。

預設使用gson

二、使用

retrofit支援同步和非同步兩種方式,在使用時,需要將請求位址轉換為介面,通過註解來指定請求方法,請求引數,請求頭,返回值等資訊。還是使用之前的person的那段json值,get請求到伺服器後從資料庫查詢資料,返回值為查詢到的資料,post請求向伺服器提交一條資料,返回值為提交的資料。 

第一步:gradle檔案中進行配置

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta2'

compile 'com.squareup.retrofit2:converter-scalars:2.0.0'

第二步:定義乙個介面,介面中使用註解註明請求方式,及對應的請求路徑

public inte***ce projectapi
get和

post請求方式的封裝,引數通過map集合的方式進行傳遞

}第二步:方法呼叫

get方式

方式

mapmap=new hashmap<>();

retrofit網路請求框架

android studio可引用gradle compile 整合retrofit compile com.squareup.retrofit2 retrofit 2.1.0 首先建立乙個inte ce請求介面 註解對應的包如下 import retrofit2.http.path 以下請求介面 ...

快捷實現網路請求Retrofit

第一步,在 android studio目錄下build.gradle中新增 compile com.squareup.retrofit2 retrofit insert latest version 第二步,建立介面api,demoapi工具類 demoapi api util public cl...

retrofit簡單的網路請求

目前retrofit速度最快,接下來是okhttp,最後是volley。定義乙個介面 public inte ce service 現在是模擬get請求,get後面帶的引數是網頁的子目錄,比如,www.baidu.com api cook list,那get後面就帶 api cook list 而b...