国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Retrofit使用總結(jié)

2019-11-09 16:09:19
字體:
供稿:網(wǎng)友

開篇

現(xiàn)在市面上已經(jīng)有了很多關(guān)于Retrofit的使用教程的博文,本篇只是我自己學(xué)習(xí)使用Retrofit時(shí)候的一個(gè)總結(jié)。本文參考自:霍丙乾 騰訊Bugly的《深入淺出 Retrofit,這么牛逼的框架你們還不來看看?》一文。

附:Retrofit首頁及官方教程

使用:

導(dǎo)入依賴:

使用首先要導(dǎo)入Retrofit的依賴:

//這兩個(gè)依賴庫的版本必須要保持一致compile 'com.squareup.retrofit2:retrofit:2.1.0'//Retrofit依賴compile 'com.squareup.retrofit2:converter-gson:2.1.0'//Retrofit數(shù)據(jù)轉(zhuǎn)換的依賴,可以直接將json轉(zhuǎn)化為實(shí)體類

在新建項(xiàng)目并建立依賴之后就可以開始使用Retrofit了。

建立一個(gè)用于發(fā)送請求的Service:

這里使用聚合數(shù)據(jù)提供的接口來獲取數(shù)據(jù):

請求地址:http://op.juhe.cn/onebox/weather/query 請求參數(shù):cityname=%E5%8C%97%E4%BA%AC&dtype=json&key=19f4708ebed559eac920bbaa51b24a12 請求方式:GET

package com.wei.retrofitdemo.Interface;import com.wei.retrofitdemo.javaBean.WeatherInform;import retrofit2.Call;import retrofit2.http.GET;import retrofit2.http.Query;/** * Created by WQC on 2016/7/15. */public interface WeatherService { //用于指定請求的方式 @GET("weather/query") //具體的請求回調(diào)的方法,可以使用@Query注解來添加url中的參數(shù) Call<WeatherInform> getWeather(@Query("cityname") String cityname, @Query("dtype") String dtype, @Query("key") String key);}

這里邊用到了WeatherInform這個(gè)JavaBean,可以使用AS的GsonFormat將json數(shù)據(jù)轉(zhuǎn)換成對應(yīng)的Java類:

(因?yàn)閖son轉(zhuǎn)成的Java類太長,放在文件中供下載查看) WeatherInform.java類下載地址

初始化Retrofit:

//baseUrl用于和之前在Service的Get注解中寫的路徑拼接成一個(gè)完整的url//addConverterFactory()可以直接將得到的json數(shù)據(jù)轉(zhuǎn)換為Service類中Call定義的WeatherInform實(shí)體類,完成了json到實(shí)體類的自動轉(zhuǎn)換 mRetrofit = new Retrofit.Builder() .baseUrl("http://op.juhe.cn/onebox/") .addConverterFactory(GsonConverterFactory.create()) .build();

請求的Url

Url的配置方式有多種,而且這些方式可以混合使用,但是最好只用一種,不然會頭暈的。

path 是絕對路徑的形式: path = “/path”,baseUrl = “URL:http://host:port/a/b” Url = “URL:http://host:port/path”

path 是相對路徑,baseUrl 是目錄形式: path = “path”,baseUrl = “URL:http://host:port/a/b/” Url = “URL:http://host:port/a/b/path”

path 是相對路徑,baseUrl 是文件形式: path = “path”,baseUrl = “URL:http://host:port/a/b” Url = “URL:http://host:port/a/path”

path 是完整的 Url: path = “http://host:port/aa/path“,baseUrl = “http://host:port/a/b” Url = “http://host:port/aa/path”

以本文使用的URL為例(使用的是第二種方式):

path : “weather/query” baseUrl:”http://op.juhe.cn/onebox/”

所以最終的Url就是:“http://op.juhe.cn/onebox/weather/query“

實(shí)例化一個(gè)天氣請求的實(shí)例:

mWeatherService = mRetrofit.create(WeatherService.class);

在需要的時(shí)候請求數(shù)據(jù):

1. 異步:

mWeatherCall = mWeatherService.getWeather("北京","json","19f4708ebed559eac920bbaa51b24a12"); mWeatherCall.enqueue(new Callback<WeatherInform>() { @Override public void onResponse(Call<WeatherInform> call, Response<WeatherInform> response) { if (response != null) { WeatherInform weatherInform = response.body(); if (weatherInform != null) { Log.i(TAG, "onResponse: " + weatherInform.getReason()); } }else{ Log.i(TAG, "onResponse: response is null"); } } @Override public void onFailure(Call<WeatherInform> call, Throwable t) { Log.i(TAG, "onFailure: "); } });

2. 同步

try { Response<WeatherInform> weatherInform = mWeatherCall.execute(); } catch (IOException e) { e.PRintStackTrace(); }

以上就是Retrofit的簡單使用方法,他使用okhttp作為底層網(wǎng)絡(luò)請求方式,在上層進(jìn)行封裝,使用注解的方式來使用。Retrofit還可以配合RxJava,RxAndroid來使用,提供更加簡潔的網(wǎng)絡(luò)請求方式。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临汾市| 岢岚县| 双牌县| 固安县| 名山县| 宜宾县| 政和县| 武清区| 白银市| 六枝特区| 通辽市| 罗田县| 南部县| 托克托县| 临漳县| 乌兰察布市| 长海县| 高安市| 富平县| 邛崃市| 万山特区| 苏州市| 靖宇县| 拉孜县| 明溪县| 塔城市| 二连浩特市| 大邑县| 金塔县| 房产| 乌拉特后旗| 宾川县| 永和县| 新宾| 马鞍山市| 乐陵市| 和硕县| 庆元县| 潮安县| 界首市| 花莲市|